在ASP.NET Core 2.1中将Value Object作为无效对象 [英] Value Object as Invalid Object in asp.net core 2.1

查看:381
本文介绍了在ASP.NET Core 2.1中将Value Object作为无效对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在asp.net core 2.0项目中使用value对象,该项目已在该项目上正常运行.

I have been using value object in asp.net core 2.0 project, which was running properly on that project.

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

I updated the project to 2.1 and it is giving me an error as

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的实现与

The implementation for ValueObject is exact same from the 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);

}

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和RemovePluralizingTableNameConvention:

OnDeleteCascading and 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版本出现问题还是实现中缺少某些内容?

What could be the reason for the problems? Is it the problem with the Entity Framework version or something missing on the implementations?

推荐答案

在EF Core版本之间,实现中始终会有一些更改.有些可能是错误修复,可能会导致旧代码以不同的方式运行.

There are always some changes in the implementation between EF Core versions. Some could be a bug fixes, which can cause the old code running differently.

问题是此代码:

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

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

First, you should exclude owned types (remember owned types are still entities in EF Core, hence are included in GetEntityTypes()):

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

否则,您将更改EF Core的默认行为,即不为拥有的实体创建单独的表(所谓的表拆分)以实际创建单独的表,因此,当EF Core构建连接到SQL Server的SQL查询时会遇到异常.表不存在.

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.

第二,您应该在所有流畅的配置之后调用该代码,因为在开始时尚未标识出所拥有的实体(如果未使用[Owned]属性标记的话)-发生了仅在OwnsOne通话之后.

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.

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

    base.OnModelCreating(modelBuilder);

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

这篇关于在ASP.NET Core 2.1中将Value Object作为无效对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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