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

7

我已经从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项目存储库上发布了一个问题。
1个回答

8

WithOwner 流畅API尚未记录(对于预览软件来说很正常),但它遵循关系API(HasOne / HasMany / WithOne, WithMany)模式用于导航属性 - 如果您有导航属性,请传递Lambda表达式或属性的名称(字符串)。如果您没有导航属性,则不需要传递任何内容。

您可以看到,对于其中一个WithOwner重载,使用Go To Definition命令是在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智能感知也显示了相同的内容。

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

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

伊万,谢谢!我已经扩展了我的问题描述——这个问题比我之前想象的要深刻。 - Dmitriy
我尝试添加了您的代码modelBuilder.Entity<Car>().OwnsOne(e => e.Volume).WithOwner(),包括和不包括ContextSnapshot代码(已注释)- 没有效果,复杂类型仍未映射... - Dmitriy
2
新问题与映射无关 - 它们已经正确映射、正确保存,但未被加载(刚刚进行了检查)。最后一个当然是一个错误。对于预览软件来说,这是正常的(或者说是预期的),你不能期望它能够正常工作。如果那是最初的问题,我甚至不会去查看或发布答案。请恢复问题更新,它必须是一个新问题(或无问题 - 请参阅有关预览软件的评论)。 - Ivan Stoev
1
是的,Ivan,我会关闭这个问题 - 你已经给出了完整准确的答案。现在我想把它贴到社区中进行错误修复(在Preview5中它运行正常)。 - Dmitriy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接