2016-11-09 171 views
1

这个代码在MVC,我需要做这样的事情,但在ASP.net Core,使用Entity Framework Core,或DataAnnotations(我已经从DbModelBuilder(MVC Entity Framework)改变参数ModelBuilder(Entity Framework Core)OnModelCreating实体框架的核心

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

一个好的起点 - [EF核心与EF6.x](https://docs.efproject.net/en/latest/efcore-vs-ef6/index.html#) –

+0

@IvanStoev找不到页面。即使我想使用配置在我的核心上下文文件中添加地图。将映射写入同一个文件看起来很乱。有没有办法,或者需要像AlexGh所做的那样添加它。 – nakulchawla09

+0

@ nakulchawla09最近的文档是[here](https://docs.microsoft.com/en-us/ef/efcore-and-ef6/)。简而言之,EF6中仍然不支持惯例和/或EF内核的配置(AFAIK有计划添加,但我不知道何时),所以现在你只能使用'modelBuilder'。 –

回答

0

直到有用于ModelBuilder.Configurations我效仿前辈实体框架建设与扩展方法支持:

public static EntityTypeBuilder<Project> Map(this EntityTypeBuilder<Project> cfg) 
{ 
    cfg.ToTable("sql_Projects"); 

    // Primary Key 
    cfg.HasKey(p => p.Id); 

    cfg.Property(p => p.Id) 
     .IsRequired() 
     .HasColumnName("ProjectId"); 

    return cfg; 
} 

,然后从OnModelCreating这样称呼它...

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Project>().Map(); 
    base.OnModelCreating(modelBuilder); 
} 

这是一个有点笨拙,但更清洁,我认为,比尝试配置几十个主DbContext中的实体。

+0

这种方法似乎并不存在。 –

+0

你指的是哪一种方法? – Neilski