EF Core 3.0 中的拥有类型映射问题

Problem with owned types mapping in EF Core 3.0

我已经从 EF Core Preview5 迁移到 Preview7,现在我有相同的内部复杂属性映射 select。

例如:

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://docs.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 属性在实验中实际上没有任何作用)。

OrderStoreDbContextpublic DbSet<OrderProfile> OrderProfiles { get; set; } 属性.

但是 linq 请求 var orderProfiles = await orderDbContext.OrderProfiles.ToListAsync(); 只映射简单类型(它们存在于 OrderProfiles table 中,但并不复杂。 var orderProfiles = await orderDbContext.OrderProfiles.Include(p => p.Volume).ToListAsync(); 代码也没有效果 - 我得到 orderProfiles.VolumeorderProfiles.StartPoint 以及 orderProfiles.EndPoint 值作为 null

但是,在 Preview5 中,这段代码工作正常。是 Microsoft 开发人员破坏了 EF Core 3.0 Preview7 中的复杂类型映射还是我弯曲的手的问题?

更新二。 在 github 项目回购上发布了一个问题。

WithOwner fluent API 仍未记录(预览软件正常),但它遵循关系 API (HasOne / HasMany / WithOne, WithMany) 导航模式 属性 - 如果您有导航 属性,请传递 lambda 表达式或 属性(字符串)的名称。如果您没有导航 属性,请不要传递任何内容。

您可以看到,使用转到定义命令的 WithOwner 重载之一是 VS:

//
// Summary:
//     Configures the relationship to the owner.
//     Note that calling this method with no parameters will explicitly configure this
//     side of the relationship to use no navigation property, even if such a property
//     exists on the entity type. If the navigation property is to be used, then it
//     must be specified.
//
// Parameters:
//   ownerReference:
//     The name of the reference navigation property pointing to the owner. If null
//     or not specified, there is no navigation property pointing to the owner.
//
// Returns:
//     An object that can be used to configure the relationship.
public virtual OwnershipBuilder<TEntity, TDependentEntity> WithOwner([CanBeNullAttribute] string ownerReference = null);

VS Intellisense 显示相同。

因此在您的情况下,只需使用 WithOwner(),例如

modelBuilder.Entity<Car>().OwnsOne(e => e.Volume).WithOwner()
    . /* configuration goes here */