您现在的位置是:网站首页> 编程资料编程资料

ASP.NET Core使用EF创建关系模型_实用技巧_

2023-05-24 290人已围观

简介 ASP.NET Core使用EF创建关系模型_实用技巧_

1.关系

关系定义两个实体之间的关系。在关系型数据库中,这由外键约束表示。

2.术语定义

有许多术语用于描述关系:

  • 相关实体:这是包含外键属性的实体。有时称为关系的"子级"。
  • 主体实体:这是包含主/备用键属性的实体。有时称为关系的 "父项"。
  • 外键:依赖实体中的属性,用于存储与实体相关的主体键属性的值。
  • 主体密钥:唯一标识主体实体的属性。这可能是主键或备用密钥。
  • 导航属性:在主体和/或从属实体上定义的属性,该属性包含对相关实体的引用。
  • 集合导航属性:一个导航属性,其中包含对多个相关实体的引用。
  • 引用导航属性:保存对单个相关实体的引用的导航属性。
  • 反向导航属性:讨论特定导航属性时,此术语是指关系另一端的导航属性。

下面的代码列表显示了与之间Blog的一对多关系Post

  • Post是依赖实体
  • Blog是主体实体
  • Post.BlogId为外键
  • Blog.BlogId是主体键(在这种情况下是主键,而不是备用键)
  • Post.Blog是一个引用导航属性
  • Blog.Posts是集合导航属性
  • Post.Blog是的Blog.Posts反向导航属性(反之亦然)
public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List 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; } }

3.约定

按照约定,当发现类型上有导航属性时,将创建关系。如果属性指向的类型不能由当前的数据库提供程序映射为标量类型,则该属性视为一个导航属性。

4.完全定义的关系

关系最常见的模式是在关系两端定义导航属性,在依赖实体类中定义外键属性。
如果在两个类型之间找到一对导航属性,则这些属性将配置为同一关系的反向导航属性。
如果依赖实体包含名为的属性,则该属性将被配置为外键。

public class Blog { public int BlogId { get; set; }  public string Url { get; set; } //导航属性 public List 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; } }

5.无外键属性

尽管建议在依赖实体类中定义外键属性,但这并不是必需的。如果未找到外键属性,则会以该名称引入阴影外键属性。

public class Blog { public int BlogId { get; set; } public string Url { get; set; } //阴影导航属性 public List 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; } }

6.单个导航属性

只包含一个导航属性(无反向导航,没有外键属性)就足以具有约定定义的关系。 还可以有一个导航属性和一个外键属性。

public class Blog { public int BlogId { get; set; } public string Url { get; set; } //阴影导航属性 public List Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } }

7.数据注释

可以使用两个数据批注来配置关系[ForeignKey]和[InverseProperty]。System.ComponentModel.DataAnnotations.Schema命名空间中提供了这些项。

7.1ForeignKey

你可以使用数据批注来配置应用程序作给定关系的外键属性的属性。通常,当不按约定发现外键属性时,会执行此操作。

namespace EFModeling.DataAnnotations.Relationships.ForeignKey { class MyContext : DbContext { public DbSet Blogs { get; set; } public DbSet Posts { get; set; } } #region Entities public class Blog { public int BlogId { get; set; } public string Url { get; set; } //导航属性 public List Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } //外键 public int BlogForeignKey { get; set; } //设置反向导航外键 [ForeignKey("BlogForeignKey")] public Blog Blog { get; set; } } #endregion }

7.2InverseProperty

您可以使用数据批注来配置依赖项和主体实体上的导航属性如何配对。这通常在两个实体类型之间存在多个导航属性对时执行。

namespace EFModeling.DataAnnotations.Relationships.InverseProperty { class MyContext : DbContext { public DbSet Posts { get; set; } public DbSet Users { get; set; } } #region Entities public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int AuthorUserId { get; set; } public User Author { get; set; } public int ContributorUserId { get; set; } public User Contributor { get; set; } } public class User { public string UserId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [InverseProperty("Author")] public List AuthoredPosts { get; set; } [InverseProperty("Contributor")] public List ContributedToPosts { get; set; } } #endregion }

8.Fluent API

若要在熟知的API中配置关系,请首先标识构成关系的导航属性。HasOne或HasMany标识要开始配置的实体类型上的导航属性。然后,将调用链接到WithOne或WithMany以标识反向导航。HasOne/WithOne用于引用导航属性,HasMany / WithMany用于集合导航属性。

namespace EFModeling.FluentAPI.Relationships.NoForeignKey { #region Model class MyContext : DbContext { public DbSet Blogs { get; set; } public DbSet Posts { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() //配置一对多关系 .HasOne(p => p.Blog) .WithMany(b => b.Posts); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List 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; } } #endregion }

8.1单个导航属性

如果只有一个导航属性,则用WithOne、WithMany的无参数重载。这表示在概念上,关系的另一端有一个引用或集合,但实体类中不包含导航属性。

namespace EFModeling.FluentAPI.Relationships.OneNavigation { #region Model class MyContext : DbContext { public DbSet Blogs { get; set; } public DbSet Posts { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() //配置多对一关系 .HasMany(b => b.Posts) .WithOne(); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } //导航属性 public List Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } } #endregion }

8.2ForeignKey

你可以使用API来配置应用程序的外键属性。

namespace EFModeling.Configuring.DataAnnotations.Samples.Relationships.ForeignKey { #region Model class MyContext : DbContext { public DbSet Blogs { get; set; } public DbSet Posts { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() //配置一对多关系 .HasOne(p => p.Blog) .WithMany(b => b.Posts) //配置外键 .HasForeignKey(p => p.BlogForeignKey); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } //导航属性 public List Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } //外键 public int BlogForeignKey { get; set; } public Blog Blog { get; set; } } #endregion }

下面的代码列表演示如何配置复合外键:

namespace EFModeling.Configuring.DataAnnotations.Samples.Relationships.CompositeForeignKey { #region Model class MyContext : DbContext { public DbSet Cars { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() //配置复合主键 .HasKey(c => new { c.State, c.LicensePlate }); modelBuilder.Entity() //配置一对多关系 .HasOne(s => s.Car) .WithMany(c => c.SaleHistory) //配置外键 .HasForeignKey(s => new { s.CarState, s.CarLicensePlate }); } } public class Car { public string State { get; set; } public string LicensePlate { get; set; } public string Make { get; set; } public string Model { get; set; } //导航属性 public List SaleHistory { get; set; } } public class RecordOfSale { public int RecordOfSaleId { get; set; } public DateTime DateSold { get; set; } public decimal Price { get; set; } //State对应CarState public string CarState { get; set; } //LicensePlate 对应CarLicensePlate public string CarLicensePlate { get; set; } public Car Car { get; set; } } #endregion }

您可以使用的HasForeignKey(...)字符串重载将影子属性配置为外键。建议先将影子属性显式添加到模型,然后再将其用作外键:

class MyContext : DbContext { public DbSet Blogs { get; set
                
                

-六神源码网