2 个不同类中使用的拥有类型的 EF Core 配置问题

Dav*_*ett 4 c# mysql ef-core-2.2

我正在使用实体框架核心,我想在 2 个不同的类中使用相同的拥有类型。这通常很好,但就我而言,我遇到了错误。

我正在使用 MySql 数据库,要求所有布尔值都映射到数据库中列类型为tinyint(1) 的字段。为了在我的 OnModelCreating 方法中实现这一点,我循环遍历所有属性,如果属性是布尔值,我将其映射到tinyint(1)。然而,一旦我在两个不同的类中使用相同的拥有类型,我就会收到错误。

下面我编写了一个演示程序来显示我的问题。重新创建它所需的只是 2 个表、组织和联系人。两者都有字段 id、街道和家庭。为了使用 MySQL,我安装了 nuget 包 MySql.Data.EntityFrameworkCore (v8.0.17)。我已在 .net core 2.2 控制台应用程序中运行代码。

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace MyDemo
{
    class Program
    {
        static void Main(string[] args)
        {
           using(var ctx = new MyDbContext())
            {
                var contact = new Contact
                {                
                    Address = new Address
                    {
                        Street = "x",
                        Home = true
                    }
                };
                ctx.Contacts.Add(contact);
                ctx.SaveChanges();
            }
        }
    }


    public class MyDbContext: DbContext
    {
        public MyDbContext()        
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL("{my connection string}");                
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Contact>()
                .OwnsOne(p => p.Address,
                a =>
                {
                    a.Property(p => p.Street)
                    .HasColumnName("street")
                    .HasDefaultValue("");
                    a.Property(p => p.Home)
                    .HasColumnName("home")
                    .HasDefaultValue(false);
                });

            modelBuilder.Entity<Organisation>()
                .OwnsOne(p => p.Address,
                a =>
                {
                    a.Property(p => p.Street)
                    .HasColumnName("street")
                    .HasDefaultValue("");
                    a.Property(p => p.Home)
                    .HasColumnName("home")
                    .HasDefaultValue(false);
                });

            var entityTypes = modelBuilder.Model.GetEntityTypes()          
            .ToList();

            foreach (var entityType in entityTypes)
            {
                var properties = entityType
                    .GetProperties()
                    .ToList();


                foreach (var property in properties)
                {
                    if (property.PropertyInfo == null)
                    {
                        continue;
                    }

                    if (property.PropertyInfo.PropertyType.IsBoolean())
                    {
                        modelBuilder.Entity(entityType.ClrType)
                        .Property(property.Name)
                        .HasConversion(new BoolToZeroOneConverter<short>())
                        .HasColumnType("tinyint(1)");
                    }
                }
            }

            base.OnModelCreating(modelBuilder);
        }

        public DbSet<Contact>Contacts { get; set; }
        public DbSet<Organisation>Organisations { get; set; }
    }

    public class Contact
    {
        public int Id { get; set; }
        public Address Address { get; set; }

        //other contact fields
    }

    public class Organisation
    {
        public int Id { get; set; }
        public Address Address { get; set; }

        //other organisation fields
    }

    public class Address
    {
        public string Street { get; set; }
        public bool Home{ get; set; }
    }

    public static class TypeExtensions
    {
        public static bool IsBoolean(this Type type)
        {
            Type t = Nullable.GetUnderlyingType(type) ?? type;
            return t == typeof(bool);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

运行上述代码后,显示的错误消息为 System.InvalidOperationException:“无法将实体类型“地址”添加到模型中,因为已存在同名的弱实体类型”。引发错误的代码部分是这一点

if (property.PropertyInfo.PropertyType.IsBoolean())
{
     modelBuilder.Entity(entityType.ClrType)
    .Property(property.Name)
    .HasConversion(new BoolToZeroOneConverter<short>())
    .HasColumnType("tinyint(1)");
}
Run Code Online (Sandbox Code Playgroud)

如何更改我的代码,以便 OnModelCreating 方法运行时不会出现错误,从而将联系人记录正确保存到数据库中?

Iva*_*oev 5

更新(EF Core 3.x):

仍然没有公共方法 get EntityTypeBuilder,但至少构造函数参数已被修改为IMutableEntityType类型,所以只有

using Microsoft.EntityFrameworkCore.Metadata.Builders;
Run Code Online (Sandbox Code Playgroud)

需要,现在对应的代码是

var entityTypeBuilder = new EntityTypeBuilder(entityType);
Run Code Online (Sandbox Code Playgroud)

原始版本(EF Core 2.x):

问题是ClrType不足以识别拥有的实体类型,因此modelBuilder.Entity(Type)无法用于获取EntityTypeBuilder流畅配置实体属性所需的实例。

似乎在 EF Core 2.x 中没有好的公共方法可以做到这一点,所以我只能建议使用一些 EF Core内部组件(幸运的是,在典型的内部使用警告下可以公开访问)。

你需要以下using内容:

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Run Code Online (Sandbox Code Playgroud)

第一个用于EntityTypeBuilder类,第二个用于AsEntityType()扩展方法,它使您可以访问实现 的内部类IEntityType,特别是Builder属性。

修改后的代码如下所示:

var entityTypes = modelBuilder.Model.GetEntityTypes()
    .ToList();

foreach (var entityType in entityTypes)
{
    var properties = entityType
        .GetProperties()
        .ToList();

    // (1)
    var entityTypeBuilder = new EntityTypeBuilder(entityType.AsEntityType().Builder);

    foreach (var property in properties)
    {
        if (property.PropertyInfo == null)
        {
            continue;
        }

        if (property.PropertyInfo.PropertyType.IsBoolean())
        {
            entityTypeBuilder // (2)
            .Property(property.Name)
            .HasConversion(new BoolToZeroOneConverter<short>())
            .HasColumnType("tinyint(1)");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)