[英]Entity Framework One to Many with no Collection navigation property
In Entity Framework with Fluent Configuration, I have a LeaseTrackingType
entity which has a one to many relationship with LeaseTracking
.在具有流畅配置的实体框架中,我有一个
LeaseTrackingType
实体,它与LeaseTracking
具有一对多的关系。 Where each lease tracking has a lease tracking type.每个租约跟踪都有一个租约跟踪类型。
However from a code point of view it doesn't really make sense to have a LeaseTrackings collection.然而,从代码的角度来看,拥有 LeaseTrackings 集合并没有什么意义。 Like you are never going to use this navigation property.
就像您永远不会使用此导航属性一样。
Question: How do I model the one to many relationship without LeaseTrackings collection navigation property?问题:我如何 model 没有 LeaseTrackings 集合导航属性的一对多关系?
Entities:实体:
public class LeaseTracking
{
public int Id { get; set; }
public int LeaseTrackingTypeId { get; set; }
public LeaseTrackingType LeaseTrackingType { get; set; }
}
public class LeaseTrackingType
{
public int Id { get; set; }
public string Name { get; set;}
public virtual Collection<LeaseTracking> LeaseTrackings { get; set;}
}
And mapping configuration:和映射配置:
public class LeaseTrackingConfiguration : EntityTypeConfiguration<LeaseTracking>
{
public LeaseTrackingConfiguration()
{
ToTable("LeaseTracking");
Property(entity => entity.Id);
HasRequired(entity => entity.LeaseTrackingType)
.WithMany(entity => entity.LeaseTrackings)
.HasForeignKey(entity => entity.LeaseTrackingTypeId);
}
}
public class LeaseTrackingTypeConfiguration : EntityTypeConfiguration<LeaseTrackingType>
{
public LeaseTrackingTypeConfiguration()
{
ToTable("LeaseTrackingType");
Property(entity => entity.Id);
Property(entity => entity.Name).;
}
}
in the constructor don't point it out in WithMany
function and get rid of the virtual Collection
in LeaseTrackingType
class在构造函数中不要在
WithMany
LeaseTrackingType
中的virtual Collection
public LeaseTrackingConfiguration()
{
ToTable("LeaseTracking");
Property(entity => entity.Id);
HasRequired(entity => entity.LeaseTrackingType)
.WithMany()
.HasForeignKey(entity => entity.LeaseTrackingTypeId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.