2 回答
TA贡献1921条经验 获得超9个赞
appsettings.json
"ConnectionStrings": {
"Cn": "Server=(localdb);Database=TestDB;Persist Security Info=True;User ID=sa;Password=abcd123!"
}
DbContext
public TestDbContext:DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> option) : base(option)
{
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
public DbSet<Users> Users { get; set; }
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TestDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Cn")));
}
最后,您可以在模型类库下使用代码优先方法。
- 2 回答
- 0 关注
- 191 浏览
添加回答
举报