Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 4 在实体框架代码中,首先我可以在依赖实体上没有导航属性吗_Entity Framework 4_Ef Code First - Fatal编程技术网

Entity framework 4 在实体框架代码中,首先我可以在依赖实体上没有导航属性吗

Entity framework 4 在实体框架代码中,首先我可以在依赖实体上没有导航属性吗,entity-framework-4,ef-code-first,Entity Framework 4,Ef Code First,假设以下模式 class Person{ public int Id {get;set;} public virtual ICollection<Province> Provinces {get;set;} } class Province{ public int Id {get;set;} public virtual ICollection<Person> Residents {get;set;} } 只能使用一个导航属性创建映射: class Per

假设以下模式

class Person{
  public int Id {get;set;}
  public virtual ICollection<Province> Provinces {get;set;}
}

class Province{
 public int Id {get;set;}
 public virtual ICollection<Person> Residents {get;set;}
}

只能使用一个导航属性创建映射:

class Person
{
    public int Id {get;set;}
    public virtual ICollection<Province> Provinces {get;set;}
}

class Province
{
    public int Id {get;set;}
}

public class PersonMapping : EntityConfiguration<Person>
{
     public PersonMapping()
     {
         this.HasMany(p => p.Provinces)
             .WithMany()
             .Map(m =>
             {
                 m.MapLeftKey("PersonId");
                 m.MapRightKey("ProvinceId");
                 m.ToTable("PersonProvinces");
             });
     }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configuration.Add(new PersonMapping());
}
班级人员
{
公共int Id{get;set;}
公共虚拟ICollection区域{get;set;}
}
阶级省份
{
公共int Id{get;set;}
}
公共类PersonMapping:EntityConfiguration
{
公众人物地图
{
这个.有很多(p=>p.Provinces)
.有很多
.Map(m=>
{
m、 MapLeftKey(“人格”);
m、 MapRightKey(“ProvinceId”);
m、 ToTable(“个人省份”);
});
}
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Configuration.Add(newPersonMapping());
}

使用不带参数的
WithMany()
是这里的关键。它告诉EF,关系是多对多的,但一端在模型中不作为导航集合公开。

“在使用DBML的正常EF中,我可以删除导航属性以实现这一点。”使用Code First,您可以执行相同的操作:只需从Providence类中删除集合属性。是的,对不起,我是从我目前使用的一个模型中转述出来的,这个模型实际上是一种多人关系。我已经更新了这个问题,谢谢。因为我不担心表名和映射,所以我在OnModelCreating中只使用了以下内容,然后根本不需要单独的映射类。modelBuilder.Entity();
protect void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configuration.Add(new Province.ProvinceMapping());
}
class Person
{
    public int Id {get;set;}
    public virtual ICollection<Province> Provinces {get;set;}
}

class Province
{
    public int Id {get;set;}
}

public class PersonMapping : EntityConfiguration<Person>
{
     public PersonMapping()
     {
         this.HasMany(p => p.Provinces)
             .WithMany()
             .Map(m =>
             {
                 m.MapLeftKey("PersonId");
                 m.MapRightKey("ProvinceId");
                 m.ToTable("PersonProvinces");
             });
     }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configuration.Add(new PersonMapping());
}