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

Dmi*_*riy 5 c# entity-framework-core .net-core ef-core-3.0

我已经从 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;}
}
Run Code Online (Sandbox Code Playgroud)

早些时候,代码modelBuilder.Entity<Car>().OwnsOne(e => e.Volume)工作正常,但现在它需要使用WithOwner但我无法理解(请参阅此处:https : //docs.microsoft.com/en-us/ef/core/what-is-new/ef-core -3.0/break-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");
                    });
            });
Run Code Online (Sandbox Code Playgroud)

在哪里

[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;}
}
Run Code Online (Sandbox Code Playgroud)

因此,正如我们所看到的,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.VolumeorderProfiles.StartPointorderProfiles.EndPoint值设为null.

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

更新 2. 在 github 项目 repo 上发布了一个问题。

Iva*_*oev 7

WithOwner流畅的API仍然是无证(正常的预览软件),但它遵循的关系API(HasOne/ HasMany/ WithOneWithMany)导航的特性模式-如果你有导航属性,无论是通过lambda表达式或属性的名称(字符串))。如果您没有导航属性,请不要传递任何内容。

您可以看到,WithOwner使用 Go To Definition Command的重载之一是 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);
Run Code Online (Sandbox Code Playgroud)

VS Intellisense 显示相同。

所以在你的情况下,只需使用WithOwner(),例如

modelBuilder.Entity<Car>().OwnsOne(e => e.Volume).WithOwner()
    . /* configuration goes here */
Run Code Online (Sandbox Code Playgroud)

  • 新问题与映射无关 - 它们被正确映射,正确保存,但未加载(刚刚检查过)。最后当然是一个错误。这对于预览软件来说是正常的(或预期的),你不能指望它能正常工作。如果这是最初的问题,我什至不会查看或发布答案。请恢复问题更新,它确实必须是一个新问题(或者没有问题 - 请参阅有关预览软件的评论), (2认同)