代码之家  ›  专栏  ›  技术社区  ›  jorjj

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

  •  1
  • jorjj  · 技术社区  · 6 年前

    我尝试使用Blazor的CRUD函数,并跟随一些文章来实现这一点。在本文中,有一部分我应该把我的连接放在上下文文件中,但它没有说明如何设置连接字符串。

    {
      "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 回复  |  直到 6 年前
        1
  •  6
  •   Nkosi    5 年前

    连接字符串设置假定在 应用设置.json

    应用设置.json

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

    public class UserContext : DbContext {
    
        public UserContext(DbContextOptions<UserContext> options): base(options) {
    
        }
    
        public virtual DbSet<User> tblUser { get; set; }
    
    }
    

    您可以在 Startup ConfigureServices 方法。

    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>();
        }
    }
    
        2
  •  1
  •   hiimdat95    6 年前

    你可以在文件中更改用户上下文.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);
            }
        }
    

    在档案里启动.cs

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<UserContext>(options =>
                   options.UseSqlServer(Configuration.GetConnectionString("UserDatabase"),
                       b => b.MigrationsAssembly("xxx.Data")));
        }