asp.net 核心 2.1 中的值对象为无效对象

Value Object as Invalid Object in asp.net core 2.1

我一直在 asp.net 核心 2.0 项目中使用值对象,在该项目中 运行 是正确的。

我将项目更新为 2.1,它给我一个错误

Invalid object name 'EntityAdress'.

实体:

public class Company : AuditableEntity<long>
{
    public int SalesRepId { get; set; }
    public string Name { get; set; }
    public int StatusId { get; set; }
    public EntityAdress Addresses { get; set; }
    public string BillingAddress { get; set; }
}

public class EntityAdress : ValueObject
{
    private EntityAdress() { }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }

    protected override IEnumerable<object> GetAtomicValues()
    {
        yield return Address;
        yield return City;
        yield return State;
        yield return Zip;
    }
}

ValueObject 的实现与 Link for the eshopContainer examples of value objects

完全相同

我正在为包含 DbContext

的项目使用的包
<Project Sdk="Microsoft.NET.Sdk">


  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.1.6</RuntimeFrameworkVersion>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
    <PackageReference Include="IdentityServer4.EntityFramework" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
  </ItemGroup>
</Project>

上下文:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.RemovePluralizingTableNameConvention();
    modelBuilder.OnDeleteCascading();

    modelBuilder.ApplyConfiguration(new CompanyEntityTypeConfiguraton());

    base.OnModelCreating(modelBuilder);

}

公司实体类型配置:

public class CompanyEntityTypeConfiguraton : IEntityTypeConfiguration<Company>
{
    public void Configure(EntityTypeBuilder<Company> orderConfiguration)
    {
        orderConfiguration.OwnsOne(p => p.Addresses, cb =>
        {
            cb.Property(p => p.City).HasColumnName("City");
            cb.Property(p => p.Address).HasColumnName("Address");
            cb.Property(p => p.State).HasColumnName("State");
            cb.Property(p => p.Zip).HasColumnName("Zip");
        });

    }
}

OnDeleteCascading 和 RemovePluralizingTableNameConvention:

public static class ModelBuilderExtensions
{
    public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder)
    {
        foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
        {
            entity.Relational().TableName = entity.DisplayName();
        }
    }
    public static void OnDeleteCascading(this ModelBuilder modelBuilder)
    {
        foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
        {
            relationship.DeleteBehavior = DeleteBehavior.Restrict;
        }
    }

}

问题的原因可能是什么?是 Entity Framework 版本的问题还是实施中缺少的东西?

EF Core 版本之间的实现始终存在一些变化。有些可能是错误修复,这可能会导致旧代码 运行 不同。

问题是这段代码:

public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder)
{
    foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
    {
        entity.Relational().TableName = entity.DisplayName();
    }
}

首先,您应该排除拥有类型(记住拥有类型仍然是 EF Core 中的实体,因此包含在 GetEntityTypes() 中):

modelBuilder.Model.GetEntityTypes().Where(t => !t.IsOwned())

否则,您正在更改 EF Core 默认行为,即不为拥有的实体创建单独的 table(所谓的 table 拆分)以实际创建单独的 table,因此当 EF Core 构建 SQL 查询加入不存在的 table 时出现的异常。

其次,您应该在所有流畅的配置之后调用该代码,因为在开始时拥有的实体(以防未标记[Owned]属性)不是尚未确定 - 它仅在 OwnsOne 调用后发生。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfiguration(new CompanyEntityTypeConfiguraton());

    base.OnModelCreating(modelBuilder);

    modelBuilder.RemovePluralizingTableNameConvention();
    modelBuilder.OnDeleteCascading();
}