我已经从EF Core Preview5迁移到了Preview7,现在我有一个相同的内部复杂属性映射通过选中。
例如:
public class Car
{
public Volume Volume { get; set; }
public string OtherProperty { get; set; }
}
[Owned]
public class Volume
{
public float Height { get; set; }
public float Width { get; set; }
public float Length { get; set;}
}
之前,代码
modelBuilder.Entity<Car>().OwnsOne(e => e.Volume)
可以正常工作,但现在需要使用WithOwner
,但我不理解(请参见这里:https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes)。我不能像这样使用代码:
modelBuilder.Entity<Car>().OwnsOne(e => e.Volume).WithOwner("Car")
或modelBuilder.Entity<Car>().OwnsOne(e => e.Volume).WithOwner(f => f.Car)
。有人遇到同样的问题吗?
谢谢。
更新。
我已经检查了OrderStoreDbContextModelSnapshot.cs。我在这里发布另一个完全符合上例的示例。
modelBuilder.Entity("DatabaseServiceNew.Database.Order_information.OrderProfile", b =>
{
b.HasOne("DatabaseService.Database.Order_information.Order", "Order")
.WithOne("OrderProfile")
.HasForeignKey("DatabaseServiceNew.Database.Order_information.OrderProfile", "OrderId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.OwnsOne("FoundationClasses.Technical_Classes.Volume", "Volume", b1 =>
{
b1.Property<Guid>("OrderProfileId");
b1.Property<float>("Cum");
b1.Property<float>("Height");
b1.Property<float>("Length");
b1.Property<float>("Width");
b1.HasKey("OrderProfileId");
b1.ToTable("OrderProfiles");
b1.WithOwner()
.HasForeignKey("OrderProfileId");
});
b.OwnsOne("WebFoundationClassesCore.Data_classes.GeoPoint", "EndPoint", b1 =>
{
b1.Property<Guid>("OrderProfileId");
b1.Property<string>("Address");
b1.Property<double>("Latitude");
b1.Property<double>("Longitude");
b1.HasKey("OrderProfileId");
b1.ToTable("OrderProfiles");
b1.WithOwner()
.HasForeignKey("OrderProfileId");
});
b.OwnsOne("WebFoundationClassesCore.Data_classes.GeoPoint", "StartPoint", b1 =>
{
b1.Property<Guid>("OrderProfileId");
b1.Property<string>("Address");
b1.Property<double>("Latitude");
b1.Property<double>("Longitude");
b1.HasKey("OrderProfileId");
b1.ToTable("OrderProfiles");
b1.WithOwner()
.HasForeignKey("OrderProfileId");
});
});
在哪里
[Owned, ComplexType]
public class Volume
{
public float Height { get; set; }
public float Width { get; set; }
public float Length { get; set;}
}
[Owned, ComplexType]
public class GeoPoint
{
public GeoPoint()
{
}
public GeoPoint(double latitude, double longitude, string address)
{
this.Address = address;
this.Latitude = latitude;
this.Longitude = longitude;
}
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Address { get; set;}
}
因此,我们可以看到,ContextSnapshot正确地映射了数据(在这种情况下,ComplexType属性实际上没有作用,这是经过试验证实的)。
OrderStoreDbContext有public DbSet < OrderProfile> OrderProfiles { get; set; }属性。
但是,linq请求var orderProfiles = await orderDbContext.OrderProfiles.ToListAsync();仅映射简单类型(存在于OrderProfiles表中,但不是复杂类型)。var orderProfiles = await orderDbContext.OrderProfiles.Include(p => p.Volume).ToListAsync();这段代码也没有效果-我得到orderProfiles.Volume和orderProfiles.StartPoint和orderProfiles.EndPoint值为null。
但是,在Preview5中,这段代码运行良好。是Microsoft的开发人员在EF Core 3.0 Preview7中破坏了复杂类型映射,还是我的手弯曲了?
更新2。 在Github项目存储库上发布了一个问题。