我一直在asp.net core 2.0项目中使用value对象,该项目已在该项目上正常运行。
我将项目更新为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);
}
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:
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版本出现问题还是实现中缺少某些内容?
答案 0 :(得分:4)
在EF Core版本之间,实现总是会有一些更改。有些可能是错误修复,可能会导致旧代码以不同的方式运行。
问题是此代码:
DELETE FROM Image
WHERE id IN (SELECT sub.id
FROM (SELECT im.id,
ROW_NUMBER() OVER(PARTITION BY i.id ORDER BY im.id) AS rn
FROM Item i
JOIN Image im
ON i.id = im.item_id) sub
WHERE sub.rn > 4);
首先,您应该排除拥有的类型(请记住,拥有的类型在EF Core中仍然是实体,因此包含在DELETE
中)
public static void RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder)
{
foreach (IMutableEntityType entity in modelBuilder.Model.GetEntityTypes())
{
entity.Relational().TableName = entity.DisplayName();
}
}
否则,您将更改EF Core的默认行为,即不为所拥有实体创建单独的表(所谓的表拆分)以实际创建单独的表,因此,当EF Core构建连接到SQL Server的SQL查询时会遇到异常。表不存在。
第二,您应该在所有流畅的配置之后调用该代码,因为在开始时尚未标识出所拥有的实体(如果未用GetEntityTypes()
属性标记的话)-它只会在modelBuilder.Model.GetEntityTypes().Where(t => !t.IsOwned())
个调用之后发生。
[Owned]