ASP.NET Core使用EF创建关系模型

 更新时间:2022年4月7日 21:21  点击:258 作者:暗断肠

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<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; }
}

3.约定

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

4.完全定义的关系

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

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; }
}

5.无外键属性

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

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; }
}

6.单个导航属性

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

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; }
}

7.数据注释

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

7.1ForeignKey

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

namespace EFModeling.DataAnnotations.Relationships.ForeignKey
{
    class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }
    #region Entities
    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 BlogForeignKey { get; set; }
        //设置反向导航外键
        [ForeignKey("BlogForeignKey")]
        public Blog Blog { get; set; }
    }
    #endregion
}

7.2InverseProperty

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

namespace EFModeling.DataAnnotations.Relationships.InverseProperty
{
    class MyContext : DbContext
    {
        public DbSet<Post> Posts { get; set; }
        public DbSet<User> 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<Post> AuthoredPosts { get; set; }
       
        [InverseProperty("Contributor")]
        public List<Post> 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<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; }
    }
    #endregion
}

8.1单个导航属性

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

namespace EFModeling.FluentAPI.Relationships.OneNavigation
{
    #region Model
    class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                //配置多对一关系
                .HasMany(b => b.Posts)
                .WithOne();
        }
    }
    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; }
    }
    #endregion
}

8.2ForeignKey

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

namespace EFModeling.Configuring.DataAnnotations.Samples.Relationships.ForeignKey
{
    #region Model
    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)
                //配置外键
                .HasForeignKey(p => p.BlogForeignKey);
        }
    }
    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 BlogForeignKey { get; set; }
        public Blog Blog { get; set; }
    }
    #endregion
}

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

namespace EFModeling.Configuring.DataAnnotations.Samples.Relationships.CompositeForeignKey
{
    #region Model
    class MyContext : DbContext
    {
        public DbSet<Car> Cars { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Car>()
                //配置复合主键
                .HasKey(c => new { c.State, c.LicensePlate });
                modelBuilder.Entity<RecordOfSale>()
                //配置一对多关系
                .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<RecordOfSale> 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<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Add the shadow property to the model
        modelBuilder.Entity<Post>()
             //配置外键
            .Property<int>("BlogForeignKey");
        // Use the shadow property as a foreign key
        modelBuilder.Entity<Post>()
            //配置一对多关系
            .HasOne(p => p.Blog)
            .WithMany(b => b.Posts)
            //配置外键
            .HasForeignKey("BlogForeignKey");
    }
}
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; }
}

8.3无导航属性

不一定需要提供导航属性。你可以直接在关系的一端提供外键。

namespace EFModeling.FluentAPI.Relationships.NoNavigation
{
    #region Model
    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<Blog>()
                .WithMany()
                //配置外键
                .HasForeignKey(p => p.BlogId);
        }
    }
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { 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; }
    }
    #endregion
}

9.主体密钥

如果你希望外键引用主键之外的属性,则可以使用熟知的API来配置关系的主体键属性。 配置为主体密钥的属性将自动设置为备用密钥。

class MyContext : DbContext
{
    public DbSet<Car> Cars { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RecordOfSale>()
            .HasOne(s => s.Car)
            .WithMany(c => c.SaleHistory)
            .HasForeignKey(s => s.CarLicensePlate)
            .HasPrincipalKey(c => c.LicensePlate);
    }
}
public class Car
{
    public int CarId { get; set; }
    public string LicensePlate { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }

    public List<RecordOfSale> SaleHistory { get; set; }
}
public class RecordOfSale
{
    public int RecordOfSaleId { get; set; }
    public DateTime DateSold { get; set; }
    public decimal Price { get; set; }

    public string CarLicensePlate { get; set; }
    public Car Car { get; set; }
}

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

class MyContext : DbContext
{
    public DbSet<Car> Cars { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RecordOfSale>()
            .HasOne(s => s.Car)
            .WithMany(c => c.SaleHistory)
            .HasForeignKey(s => new { s.CarState, s.CarLicensePlate })
            .HasPrincipalKey(c => new { c.State, c.LicensePlate });
    }
}
public class Car
{
    public int CarId { get; set; }
    public string State { get; set; }
    public string LicensePlate { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }

    public List<RecordOfSale> SaleHistory { get; set; }
}
public class RecordOfSale
{
    public int RecordOfSaleId { get; set; }
    public DateTime DateSold { get; set; }
    public decimal Price { get; set; }

    public string CarState { get; set; }
    public string CarLicensePlate { get; set; }
    public Car Car { get; set; }
}

10.必需和可选的关系

您可以使用熟知的API来配置是必需的还是可选的关系。最终,这会控制外键属性是必需的还是可选的。当使用阴影状态外键时,这非常有用。如果实体类中具有外键属性,则关系的requiredness取决于外键属性是必需还是可选。

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)
            .IsRequired();
    }
}
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; }
}

11.级联删除

您可以使用熟知的API显式配置给定关系的级联删除行为。

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)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
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; }
}

12.其他关系模式

12.1一对一

一对多关系在两侧都有一个引用导航属性。它们遵循与一对多关系相同的约定,但在外键属性上引入了唯一索引,以确保只有一个依赖项与每个主体相关。

12.1.1数据注释

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage { get; set; }
}
public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

12.1.2Fluent API

使用API 配置关系时,请使用HasOne和WithOne方法。配置外键时,需要指定依赖实体类型,请注意以下列表HasForeignKey中提供的泛型参数。在一对多关系中,可以清楚地表明具有引用导航的实体是依赖项,并且具有集合的实体是主体。但这并不是一对一的关系,因此需要显式定义它。

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<BlogImage> BlogImages { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasOne(p => p.BlogImage)
            .WithOne(i => i.Blog)
            .HasForeignKey<BlogImage>(b => b.BlogForeignKey);
    }
}
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage { get; set; }
}
public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogForeignKey { get; set; }
    public Blog Blog { get; set; }
}

12.2多对多

目前尚不支持多对多关系,没有实体类来表示联接表。但是,您可以通过包含联接表的实体类并映射两个不同的一对多关系,来表示多对多关系。

class MyContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Tag> Tags { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PostTag>()
            .HasKey(pt => new { pt.PostId, pt.TagId });
        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Post)
            .WithMany(p => p.PostTags)
            .HasForeignKey(pt => pt.PostId);
        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Tag)
            .WithMany(t => t.PostTags)
            .HasForeignKey(pt => pt.TagId);
    }
}
public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public List<PostTag> PostTags { get; set; }
}
public class Tag
{
    public string TagId { get; set; }

    public List<PostTag> PostTags { get; set; }
}
public class PostTag
{
    public int PostId { get; set; }
    public Post Post { get; set; }

    public string TagId { get; set; }
    public Tag Tag { get; set; }
}

到此这篇关于ASP.NET Core使用EF创建关系模型的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://www.cnblogs.com/wzk153/p/11730933.html

[!--infotagslink--]

相关文章

  • ASP.NET购物车实现过程详解

    这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • .NET Core下使用Kafka的方法步骤

    这篇文章主要介绍了.NET Core下使用Kafka的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • 在ASP.NET 2.0中操作数据之七十二:调试存储过程

    在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
  • 详解.NET Core 3.0 里新的JSON API

    这篇文章主要介绍了详解.NET Core 3.0 里新的JSON API,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • ASP.NET Core根据环境变量支持多个 appsettings.json配置文件

    这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • 记一次EFCore类型转换错误及解决方案

    这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
  • 详解为什么现代系统需要一个新的编程模型

    如今高要求的分布式系统的建造者遇到了不能完全由传统的面向对象编程(OOP)模型解决的挑战,但这可以从Actor模型中获益。...2021-05-20
  • 详解ASP.NET Core 中基于工厂的中间件激活的实现方法

    这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22
  • 解决在keras中使用model.save()函数保存模型失败的问题

    这篇文章主要介绍了解决在keras中使用model.save()函数保存模型失败的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-05-21
  • asp.net通过消息队列处理高并发请求(以抢小米手机为例)

    这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • ASP.NET单选按钮控件RadioButton常用属性和方法介绍

    RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22
  • ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表

    在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19
  • 详解.NET Core 使用HttpClient SSL请求出错的解决办法

    这篇文章主要介绍了.NET Core 使用HttpClient SSL请求出错的解决办法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-09-22
  • Underscore源码分析

    Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象。这篇文章主要介绍了underscore源码分析相关知识,感兴趣的朋友一起学习吧...2016-01-02
  • Python通过VGG16模型实现图像风格转换操作详解

    这篇文章主要介绍了Python通过VGG16模型实现图像风格转换操作,结合实例形式详细分析了Python使用VGG16模型实现图像风格转换的具体原理、操作步骤与实现方法,需要的朋友可以参考下...2020-04-27
  • ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容

    这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22
  • 创建一个完整的ASP.NET Web API项目

    ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
  • ASP.NET连接MySql数据库的2个方法及示例

    这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
  • Asp.Net使用Bulk实现批量插入数据

    这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
  • 解决Keras 中加入lambda层无法正常载入模型问题

    这篇文章主要介绍了解决Keras 中加入lambda层无法正常载入模型问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-17