简体   繁体   中英

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.

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

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

The Package i am using for the projects which contains the 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:

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:

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. 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() ):

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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