EF Relationships
Relationships, navigation properties, and foreign keys
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public Department()
{
this.Courses = new HashSet<Course>();
}
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public DateTime StartDate { get; set; }
public int? Administrator {get ; set; }
public virtual ICollection<Course> Courses { get; set; }
}
EF Relationships
A relationship defines how two entities relate to each other. In a relational database, this is represented by a foreign key constraint.
Note
Most of the samples in this article use a one-to-many relationship to demonstrate concepts. For examples of one-to-one and many-to-many relationships see the Other Relationship Patterns section at the end of the article.
Definition of terms
There are a number of terms used to describe relationships
-
Dependent entity: This is the entity that contains the foreign key properties. Sometimes referred to as the 'child' of the relationship.
-
Principal entity: This is the entity that contains the primary/alternate key properties. Sometimes referred to as the 'parent' of the relationship.
-
Principal key: The properties that uniquely identify the principal entity. This may be the primary key or an alternate key.
-
Foreign key: The properties in the dependent entity that are used to store the principal key values for the related entity.
-
Navigation property: A property defined on the principal and/or dependent entity that references the related entity.
-
Collection navigation property: A navigation property that contains references to many related entities.
-
Reference navigation property: A navigation property that holds a reference to a single related entity.
-
Inverse navigation property: When discussing a particular navigation property, this term refers to the navigation property on the other end of the relationship.
-
-
Self-referencing relationship: A relationship in which the dependent and the principal entity types are the same.
The following code shows a one-to-many relationship between Blog
and Post
一个Blog对应多个Posts,一对多的关系。 相当于上面的一个Department对应多个Course。
Post: An individual entry or article on a blog. A blog is short for weblog.
A weblog is a website consisting of entries (posts) that are displayed in reverse chronological order with the most recent post appearing first. “Blogs” refers to the universe of blogs – not to individual posts.
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
-
Post
is the dependent entity -
Blog
is the principal entity -
Blog.BlogId
is the principal key (in this case it is a primary key rather than an alternate key) -
Post.BlogId
is the foreign key -
Post.Blog
is a reference navigation property -
Blog.Posts
is a collection navigation property -
Post.Blog
is the inverse navigation property ofBlog.Posts
(and vice versa)
Conventions
By default, a relationship will be created when there is a navigation property discovered on a type. A property is considered a navigation property if the type it points to cannot be mapped as a scalar type by the current database provider.
Note
Relationships that are discovered by convention will always target the primary key of the principal entity. To target an alternate key, additional configuration must be performed using the Fluent API.
Manual configuration
Post只对应一个Blog,然后Blog有多个Posts
internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts);
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Blog Blog { get; set; }
}
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-11-15 高等数学 工专 柳重堪
2021-11-15 TortoiseGit not showing icon overlays Git图标消失
2021-11-15 Node.js: Python not found exception due to node-sass and node-gyp
2018-11-15 kentico检查当前授权用户,是否为admin角色
2016-11-15 signtool