.NET Core:如何设置连接字符串?

我正在尝试使用 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");

            }

        }

    }


Helenr
浏览 369回答 2
2回答

弑天下

假设连接字符串设置在appsetting.json文件中。将文件添加到项目中appsetting.json{&nbsp; "ConnectionStrings": {&nbsp; &nbsp; "UserDatabase": "Server=DESKTOP-2K2A6GN;Database=Assignment4;Trusted_Connection=True;"&nbsp; }}更新 DbContext 以便可以对其进行配置。public class UserContext : DbContext {&nbsp; &nbsp; public UserContext(DbContextOptions<UserContext> options): base(options) {&nbsp; &nbsp; }&nbsp; &nbsp; public virtual DbSet<User> tblUser { get; set; }}您将Startup在ConfigureServices方法的类中配置 DbContext 。public class Startup {&nbsp; &nbsp; public Startup(IHostingEnvironment env) {&nbsp; &nbsp; &nbsp; &nbsp; var builder = new ConfigurationBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SetBasePath(env.ContentRootPath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .AddJsonFile("appsettings.json");&nbsp; &nbsp; &nbsp; &nbsp; Configuration = builder.Build();&nbsp; &nbsp; }&nbsp; &nbsp; static IConfiguration Configuration { get; set; }&nbsp; &nbsp; public void ConfigureServices(IServiceCollection services) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; services.AddDbContext<UserContext>(options =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.UseSqlServer(Configuration.GetConnectionString("UserDatabase"));&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; }&nbsp; &nbsp; // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.&nbsp; &nbsp; public void Configure(IApplicationBuilder app, IHostingEnvironment env) {&nbsp; &nbsp; &nbsp; &nbsp; if (env.IsDevelopment()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.UseDeveloperExceptionPage();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; app.UseBlazor<Client.Program>();&nbsp; &nbsp; }}

aluckdog

您可以在文件 UserContext.cs 中更改public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public UserContext CreateDbContext(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IConfiguration configuration = new ConfigurationBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SetBasePath(Directory.GetCurrentDirectory())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .AddJsonFile("appsettings.json").Build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var builder = new DbContextOptionsBuilder<AppDbContext>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var connectionString = configuration.GetConnectionString("UserDatabase");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.UseSqlServer(connectionString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new AppDbContext(builder.Options);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }在文件 Startup.cs 中public void ConfigureServices(IServiceCollection services)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; services.AddDbContext<UserContext>(options =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options.UseSqlServer(Configuration.GetConnectionString("UserDatabase"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b => b.MigrationsAssembly("xxx.Data")));&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP