首页 > 其他分享 >EF Relationships

EF Relationships

时间:2022-11-15 15:37:40浏览次数:139  
标签:Relationships set get EF entity Blog property public

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.

C#
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 of Blog.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; }
}

 

标签:Relationships,set,get,EF,entity,Blog,property,public
From: https://www.cnblogs.com/chucklu/p/16892526.html

相关文章