实体框架核心约定在哪里? [英] Where are Entity Framework Core conventions?

查看:67
本文介绍了实体框架核心约定在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用EF 6.1+有时,我们需要添加或删除现有的概念。该代码看起来或多或少像:

using EF 6.1+ there were times where we need to add or remove existing conentions. The code looks more or less like:

public class MyContext : DbContext
    {
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.AddFromAssembly(Assembly.GetExecutingAssembly());
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
                modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
                modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

                base.OnModelCreating(modelBuilder);
            }
}

在EF核心中如何做? Modelbuilder没有Conventions属性:(

how do the same in EF core? Modelbuilder has no Conventions property :(

推荐答案

我正在将一些代码从EF移植到EF Core 2.1+,迫不及待对于EF Core 3.0,所以写了一些扩展方法,这对您有所帮助。.

I'm porting some code from EF to EF Core 2.1+ and can't wait for EF Core 3.0 so wrote a few extension methods which help a bit..

public static IEnumerable<IMutableEntityType> EntityTypes(this ModelBuilder builder)
{
    return builder.Model.GetEntityTypes();
}

public static IEnumerable<IMutableProperty> Properties(this ModelBuilder builder)
{
    return builder.EntityTypes().SelectMany(entityType => entityType.GetProperties());
}

public static IEnumerable<IMutableProperty> Properties<T>(this ModelBuilder builder)
{
    return builder.EntityTypes().SelectMany(entityType => entityType.GetProperties().Where(x => x.ClrType == typeof(T)));
}

public static void Configure(this IEnumerable<IMutableEntityType> entityTypes, Action<IMutableEntityType> convention)
{
    foreach (var entityType in entityTypes)
    {
        convention(entityType);
    }
}

public static void Configure(this IEnumerable<IMutableProperty> propertyTypes, Action<IMutableProperty> convention)
{
    foreach (var propertyType in propertyTypes)
    {
        convention(propertyType);
    }
}

使用这些,您可以编写类似于EF中的约定例如,6.1.x。

with these you can write conventions similar to those in EF 6.1.x, for example.

// equivalent of modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.EntityTypes()
            .Configure(et => et.Relational().TableName = et.DisplayName());

// Put the table name on the primary key
modelBuilder.Properties()
            .Where(x => x.Name == "Id")
            .Configure(p => p.Relational().ColumnName = p.DeclaringEntityType.Name + "Id");

// Mark timestamp columns as concurrency tokens
modelBuilder.Properties()
            .Where(x => x.Name == "Timestamp")
            .Configure(p => p.IsConcurrencyToken = true);

对于EF Core 3.0,元模型方法略有更改,因此您需要

For EF Core 3.0 the metamodel methods have changed slightly so you need

modelBuilder.EntityTypes()
            .Configure(et => et.SetTableName(et.DisplayName()));

modelBuilder.Properties()
            .Where(x => x.Name == "Id")
            .Configure(p => p.SetColumnName(BaseName(p.DeclaringEntityType.Name) + "Id"));

没有检查效率,但是除非您的模型很大,否则不会造成问题

Haven't checked this for efficiency but unless your model is huge it shouldn't pose a problem

可以与其他助手一起扩展外键,索引等

This can be extended with other helpers for foreign keys, indexes etc

这篇关于实体框架核心约定在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆