OnModelCreating实体框架核心

时间:2016-11-09 17:47:55

标签: c# asp.net-mvc entity-framework asp.net-core

此代码位于MVC,我需要在ASP.net Core中使用Entity Framework CoreDataAnnotations执行此类操作(我已更改了参数已从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?)

1 个答案:

答案 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中配置数十个实体。