简体   繁体   English

OnModelCreating Entity Framework Core

[英]OnModelCreating Entity Framework Core

This code is in MVC , I need to do something like this but in ASP.net Core , using Entity Framework Core , or with DataAnnotations (I've changed the parameter already from DbModelBuilder(MVC Entity Framework) to ModelBuilder(Entity Framework Core) )这段代码在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);
        }

There are some errors when I'm trying to use it in Entity Framework Core :当我尝试在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?) 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?) 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?)

Until there is support for ModelBuilder.Configurations I emulate the older Entity Framework construction with extension methods:在支持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;
}

and then call it from OnModelCreating like this...然后像这样从OnModelCreating调用它......

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

It is a bit clumsy, but cleaner, I think, than trying to configure dozens of entities within the main DbContext.与尝试在主 DbContext 中配置数十个实体相比,这有点笨拙,但我认为更简洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架和OnModelCreating() - Entity Framework and OnModelCreating() Entity Framework Core 在 OnModelCreating 中分配外键约束? - Entity Framework Core assign foreign key constraint in OnModelCreating? 以下方法或属性 [ Entity Framework Core ] [ OnModelCreating ] 之间的调用不明确 - The call is ambiguous between the following methods or properties [ Entity Framework Core ] [ OnModelCreating ] 实体框架核心OnModelCreating与Migration,正确的开发工作流程 - Entity Framework Core OnModelCreating vs Migration, the correct development workflow 将.net实体框架升级到EF Core似乎无法使用base和OnModelCreating - Upgrading .net entity framework to EF Core seems that base and OnModelCreating are not available 在依赖注入中使用 Entity Framework Core DbContext 时不调用 OnModelCreating - OnModelCreating is not called when Entity Framework Core DbContext is used in Dependency Injection 如何在Entity Framework Core 2.0的模型内部使用OnModelCreating? - How Do I Use OnModelCreating Inside the Model itself in Entity Framework Core 2.0? Entity Framework Core - 禁用 Model 缓存,为每个实例 dcontext 调用 onModelCreating() - Entity Framework Core - Disable Model caching , call onModelCreating() for each instance dcontext 实体框架在 OnModelCreating 期间检查列是否存在 - Entity Framework check if column exists during OnModelCreating 实体框架7 DbContext OnModelCreating为ApplicationUser字段指定外键 - Entity Framework 7 DbContext OnModelCreating specify foreign key for ApplicationUser field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM