C# EF Core 3.0中的自有类型映射问题
我已经从EF Core Preview5迁移到Preview7,现在我通过select实现了相同的内部复杂属性映射 例如:C# EF Core 3.0中的自有类型映射问题,c#,.net-core,entity-framework-core,ef-core-3.0,C#,.net Core,Entity Framework Core,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 fl
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().OwnsOne(e=>e.Volume)
工作正常,但现在它需要使用WithOwner
,但我无法理解(请参见此处:)
我不能使用这样的代码:modelBuilder.Entity().OwnsOne(e=>e.Volume)。WithOwner(“Car”)
或modelBuilder.Entity().OwnsOne(e=>e.Volume)。WithOwner(f=>f.Car)
。
有人有同样的问题吗
谢谢
更新
我已经检查了orderstoredContextModelSnapshot.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");
});
});
因此,正如我们所看到的,ContextSnapshot正确地映射了数据(实验上,在本例中,ComplexType属性在实际中不起任何作用)
OrderStoreDbContext
具有public DbSet OrderProfiles{get;set;}
属性
但是linq requestvar orderProfiles=await orderDbContext.orderProfiles.ToListAsync()代码>只映射简单类型(存在于OrderProfiles表中,但不复杂)。
var orderProfiles=wait orderDbContext.orderProfiles.Include(p=>p.Volume.toListSync();
code也没有效果-我得到orderProfiles.Volume
和orderProfiles.StartPoint
和orderProfiles.EndPoint
值为null
但是,在Preview5中,这段代码运行得很好。microsoft开发人员是否破坏了EF Core 3.0 Preview7中的复杂类型映射,还是我手中的问题
更新2。
在github project repo上发布了一个问题。with owner
fluent API仍然没有文档记录(预览软件正常),但它遵循关系API(HasOne
/HasMany
/with one
,with many
)导航属性的模式-如果您有导航属性,请传递lambda表达式或属性名称(字符串))。如果没有导航属性,请不要传递任何内容
您可以看到,对于使用Go To Definition命令的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);
伊万,谢谢你!我已经对我的问题进行了广泛的描述-这个问题比我以前想象的更深刻。我尝试添加您的代码modelBuilder.Entity().OwnsOne(e=>e.Volume)。WithOwner()带有和不带有ContextSnapshot代码(注释了它)-没有效果,仍然没有映射复杂类型…新问题与映射无关-它们映射正确,已正确保存,但未加载(仅检查该选项)。最后一个当然是一个bug。这对于预览软件是正常的(或预期的),您不能期望它正常工作。如果这是最初的问题,我甚至不会看或张贴答案。请回复问题更新,它确实必须是一个新问题(或者没有问题-请参阅关于预览软件的评论),是的,伊万,我将结束这个问题-您已经给出了完整准确的答案。现在我想在社区中通过它来修复bug(在Preview5中,它工作得很好)。
//
// 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);
modelBuilder.Entity<Car>().OwnsOne(e => e.Volume).WithOwner()
. /* configuration goes here */