简体   繁体   English

在ASP.NET Core 2.1中将Value Object作为无效对象

[英]Value Object as Invalid Object in asp.net core 2.1

I have been using value object in asp.net core 2.0 project, which was running properly on that project. 我一直在asp.net core 2.0项目中使用value对象,该项目已在该项目上正常运行。

I updated the project to 2.1 and it is giving me an error as 我将项目更新为2.1,它给我一个错误

Invalid object name 'EntityAdress'.

Entity: 实体:

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;
    }
}

The implementation for ValueObject is exact same from the Link for the eshopContainer examples of value objects ValueObject的实现与eshopContainer值对象示例链接完全相同

The Package i am using for the projects which contains the DbContext 我正在用于包含DbContext的项目的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>

Context: 语境:

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

    modelBuilder.ApplyConfiguration(new CompanyEntityTypeConfiguraton());

    base.OnModelCreating(modelBuilder);

}

CompanyEntityTypeConfiguraton: CompanyEntityTypeConfiguraton:

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 and RemovePluralizingTableNameConvention: 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;
        }
    }

}

What could be the reason for the problems? 问题的原因可能是什么? Is it the problem with the Entity Framework version or something missing on the implementations? 实体框架版本是否有问题或实现方面缺少某些内容?

There are always some changes in the implementation between EF Core versions. EF Core版本之间的实现始终会有一些更改。 Some could be a bug fixes, which can cause the old code running differently. 有些可能是错误修复,可能导致旧代码以不同的方式运行。

The problem is this code: 问题是此代码:

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

First, you should exclude owned types (remember owned types are still entities in EF Core, hence are included in GetEntityTypes() ): 首先,您应该排除拥有的类型(记住拥有的类型在EF Core中仍然是实体,因此包含在GetEntityTypes() ):

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

otherwise you are changing the EF Core default behavior of not creating separate table for the owned entity (the so called table splitting) to actually create a separate table, hence the exception you are getting when EF Core builds SQL query joining to the table that does not exist. 否则,您将更改EF Core的默认行为,即不为所拥有实体创建单独的表(所谓的表拆分)以实际创建单独的表,因此,当EF Core构建连接到该表的表的SQL查询时,会遇到异常不存在。

Second, you should call that code after all fluent configuration, because at the beginning the owned entities (in case are not marked with [Owned] attribute) are not identified as such yet - it happens only after OwnsOne calls. 其次,您应该所有流利的配置之后调用该代码,因为在开始OwnsOne没有这样标识所拥有的实体(如果未用[Owned]属性标记的话)-仅在OwnsOne调用之后才发生。

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

    base.OnModelCreating(modelBuilder);

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM