简体   繁体   English

没有集合导航属性的实体框架一对多

[英]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.

相关问题 实体框架订购(按导航属性)(一对多) - ENTITY FRAMEWORK OrderBy on Navigation Property (one-to-many) 实体框架6没有导航属性的一对多关系 - Entity Framework 6 One-To-Many Relationship without Navigation Property Entity Framework Core:基于导航属性的集合 - Entity Framework Core: navigation property based collection Entity Framework 6在运行时检查导航属性是多对多还是一对多的方法 - Entity Framework 6 way to check at runtime if navigation property is many-to-many or one-to-many 实体框架导航属性一对多变为一对一 - Entity Framework Navigation Property One-to-Many becomes One-to-One Entity Framework Core 在一对多关系中添加具有所需导航属性的新实体? - Entity Framework Core adding new Entity with required navigation property in one-to-many relationship? 实体框架将集合属性映射到一列 - Entity Framework map collection property to one column Entity Framework Core 多对多更改导航属性名称 - Entity Framework Core Many to Many change navigation property names 在实体框架核心的多对多关系中获取导航属性时出错 - Error in getting navigation property in a many to many relationship in entity framework core 在 Entity Framework 6 中,如何使用另一侧的导航属性创建一对多映射? - In Entity Framework 6 how can I create a one to many mapping with a navigation property on the other side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM