亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

.NET Core:如何設置連接字符串?

.NET Core:如何設置連接字符串?

C#
Helenr 2021-11-07 19:36:38
我正在嘗試使用 Blazor 的 CRUD 函數并按照一些文章來執行此操作。在文章中,有一部分我應該將我的連接放在上下文文件中,但它沒有說明如何設置連接字符串。我將此代碼行放在 launchSettings.json 中:{  "ConnectionStrings": {    "UserDatabase": "Server=DESKTOP-2K2A6GN;Database=Assignment4;Trusted_Connection=True;"  },  "iisSettings": {    "windowsAuthentication": false,    "anonymousAuthentication": true,    "iisExpress": {      "applicationUrl": "http://localhost:56244/",      "sslPort": 0    }  },  "profiles": {    "IIS Express": {      "commandName": "IISExpress",      "launchBrowser": true,      "environmentVariables": {        "ASPNETCORE_ENVIRONMENT": "Development"      }    },    "Assignment4.Server": {      "commandName": "Project",      "launchBrowser": true,      "environmentVariables": {        "ASPNETCORE_ENVIRONMENT": "Development"      },      "applicationUrl": "http://localhost:56248/"    }  }}我嘗試將連接字符串添加到上下文文件中,但沒有成功。public class UserContext : DbContext    {        public virtual DbSet<User> tblUser { get; set; }        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        {            if (!optionsBuilder.IsConfigured)            {                optionsBuilder.UseSqlServer(@"UserDatabase");            }        }    }
查看完整描述

2 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

假設連接字符串設置在appsetting.json文件中。將文件添加到項目中


appsetting.json


{

  "ConnectionStrings": {

    "UserDatabase": "Server=DESKTOP-2K2A6GN;Database=Assignment4;Trusted_Connection=True;"

  }

}

更新 DbContext 以便可以對其進行配置。


public class UserContext : DbContext {


    public UserContext(DbContextOptions<UserContext> options): base(options) {


    }


    public virtual DbSet<User> tblUser { get; set; }


}

您將Startup在ConfigureServices方法的類中配置 DbContext 。


public class Startup {


    public Startup(IHostingEnvironment env) {


        var builder = new ConfigurationBuilder()

            .SetBasePath(env.ContentRootPath)

            .AddJsonFile("appsettings.json");


        Configuration = builder.Build();

    }


    static IConfiguration Configuration { get; set; }


    public void ConfigureServices(IServiceCollection services) {    


        services.AddDbContext<UserContext>(options =>

            options.UseSqlServer(Configuration.GetConnectionString("UserDatabase"));

        );


        //...

    }


    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {

        if (env.IsDevelopment()) {

            app.UseDeveloperExceptionPage();

        }


        app.UseBlazor<Client.Program>();

    }

}


查看完整回答
反對 回復 2021-11-07
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

您可以在文件 UserContext.cs 中更改


public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>

    {

        public UserContext CreateDbContext(string[] args)

        {

            IConfiguration configuration = new ConfigurationBuilder()

                .SetBasePath(Directory.GetCurrentDirectory())

                .AddJsonFile("appsettings.json").Build();

            var builder = new DbContextOptionsBuilder<AppDbContext>();

            var connectionString = configuration.GetConnectionString("UserDatabase");

            builder.UseSqlServer(connectionString);

            return new AppDbContext(builder.Options);

        }

    }

在文件 Startup.cs 中


public void ConfigureServices(IServiceCollection services)

    {

        services.AddDbContext<UserContext>(options =>

               options.UseSqlServer(Configuration.GetConnectionString("UserDatabase"),

                   b => b.MigrationsAssembly("xxx.Data")));

    }


查看完整回答
反對 回復 2021-11-07
  • 2 回答
  • 0 關注
  • 400 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號