对于我们的ASP.NET Core项目,我们使用Package Manger控制台中的Scaffold-DbContext来构建现有数据库。
每次我们进行脚手架操作时,都会与所有实体一起生成一个上下文类,它包含OnConfiguring(..)方法,该方法调用optionsBuilder.UseSqlServer(..)来配置上下文以连接到SQL Server数据库
我们在appsettings.json文件中定义了连接字符串,并且不想使用OnConfiguring(..)方法,所以问题是,如果有办法自动删除OnConfiguring(.. )方法,这样我们每次运行脚手架时都不必手动完成。
答案 0 :(得分:2)
仅供参考,EF Core 5.0中提供了一个-NoOnConfiguring
选项
-NoOnConfiguring Don't generate DbContext.OnConfiguring. Added in EF Core 5.0.
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext
答案 1 :(得分:0)
首先。我建议您使用dotnet核心迁移cli来搭建数据库。可以在here中找到命令的详细信息。
使用dotnet核心迁移CLI,您只需要像这样在applicationdbcontext类中定义配置
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
modelBuilder.Entity<User>().HasMany(u => u.Claims).WithOne().HasForeignKey(c => c.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<User>().HasMany(u => u.Roles).WithOne().HasForeignKey(r => r.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Claims).WithOne().HasForeignKey(c => c.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Users).WithOne().HasForeignKey(r => r.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade);
modelBuilder.EnableAutoHistory(null);
modelBuilder.Entity<Post>()
.HasOne(p => p.Medias)
.WithOne(m => m.Post)
.HasForeignKey<Media>(p => p.PostId);
}
您只需要像这样在startup.cs文件中定义连接
services.AddDbContextPool<ApplicationDbContext>(options =>
options.UseLazyLoadingProxies().UseSqlServer(configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("AwesomeCMSCore")).UseOpenIddict());
希望我对你说清楚。如果您有任何问题,请告诉我
答案 2 :(得分:0)
您正在搜索的选项是:--no-onconfiguring
curl -v 'https://developer.api.autodesk.com/authentication/v1/authenticate' \
-X 'POST' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'client_id=6BfkncD9ypGMiHkjrfka5ydqrG4GLx1z&
client_secret=EC5i7QG61Qg8jDmZ&
grant_type=client_credentials&
scope=data:read
'