Entity framework EF:在属性中声明多对多关系
在fluentapi中可以这样声明多对多关系Entity framework EF:在属性中声明多对多关系,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,在fluentapi中可以这样声明多对多关系 modelBuilder.Entity<Course>() .HasMany(t => t.Instructors) .WithMany() modelBuilder.Entity() .HasMany(t=>t.讲师) .有很多 我宁愿在我的域模型中有某些属性也可以这样做。 框架中是否存在这样的属性,或者我是否可以自己创建一些属性,这些属性在生成数据库时会影响EF行为 我假设了你的模型,试试下面,看看它是否有效
modelBuilder.Entity<Course>()
.HasMany(t => t.Instructors)
.WithMany()
modelBuilder.Entity()
.HasMany(t=>t.讲师)
.有很多
我宁愿在我的域模型中有某些属性也可以这样做。
框架中是否存在这样的属性,或者我是否可以自己创建一些属性,这些属性在生成数据库时会影响EF行为 我假设了你的模型,试试下面,看看它是否有效
public class Course
{
[InverseProperty("Courses")] //add this attribute
public ICollection<Instructor> Instructors { get; set; }
//other properties..
}
public class Instructor
{
[InverseProperty("Instructors")] //add this attribute
public ICollection<Course> Courses { get; set; }
//other properties..
}
公共课
{
[InverseProperty(“课程”)]//添加此属性
公共ICollection讲师{get;set;}
//其他财产。。
}
公开课教师
{
[InverseProperty(“Instructors”)]//添加此属性
公共ICollection课程{get;set;}
//其他财产。。
}
这样,您可以告诉实体框架在课程
模型中为讲师
查找要映射的属性
此外,你甚至不需要这样定义。但是,如果您在讲师
中有多个课程
类型的属性,或者反之亦然,则需要正确指出哪些属性映射到哪些属性
尽管如此,使用fluentapi还是更好、更具可扩展性和可管理性的您不需要任何属性。如果在每个实体中声明
ICollection
,EF将按照约定创建多对多关系。看