此代码位于MVC
,我需要在ASP.net Core
中使用Entity Framework Core
或DataAnnotations
执行此类操作(我已更改了参数已从DbModelBuilder(MVC Entity Framework)
到ModelBuilder(Entity Framework Core)
)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); //error: Conventions is not recognized
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<tbl_Line>()
.HasMany(d => d.tbl_Policy)
.WithRequired(c => c.tbl_Line) //error WithRequired not recognized
.HasForeignKey(c => c.int_lineID);
}
我在Entity Framework Core
尝试使用它时出现了一些错误:
1 - 'ModelBuilder' does not contain a definition for 'Conventions' and no extension method 'Conventions' accepting a first argument of type 'ModelBuilder' could be found (are you missing a using directive or an assembly reference?)
2 - 'CollectionNavigationBuilder<tbl_Line, tbl_Policy>' does not contain a definition for 'WithRequired' and no extension method 'WithRequired' accepting a first argument of type 'CollectionNavigationBuilder<tbl_Line, tbl_Policy>' could be found (are you missing a using directive or an assembly reference?)
答案 0 :(得分:0)
在支持 ModelBuilder.Configurations 之前,我使用扩展方法模拟旧的 Entity Framework 构造:
$("#viewOrHideDetail_" + Id).html("Hide Details <i class='fa fa-chevron-up' aria-hidden='true'></i>");
^^^^
check here
然后从 OnModelCreating 这样调用它......
fa-chevron-up
我认为它有点笨拙但更干净,而不是尝试在主DbContext中配置数十个实体。