zoukankan      html  css  js  c++  java
  • EF Core 数据库实体关系外键的配置

    一、EF Core 默认约定的导航属性

    1、如果两个类型之间找到一对导航属性,则它们将被配置为同一关系的反转导航属性。

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

    2.依赖实体(多端的实体)包含名称与其中一种模式相匹配的属性,则该属性将被配置为外键: 

    a.依赖主体设置:导航属性、外键属性(导航属性名+主体主键名) 

    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 BlogModelBlogId { get; set; }
        public Blog BlogModel { get; set; }
      }

    a.依赖主体设置:导航属性、外键属性(导航属性名+Id) 

    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 BlogModelBlogId { get; set; }
        public Blog BlogModel { get; set; }
      }

    c.依赖主体设置:导航属性、外键属性(主体类型名+主体主键名)

    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 BlogBlogId { get; set; }
        public Blog BlogModel { get; set; }
      }

    d.依赖主体设置:导航属性、外键属性(主体类型名+Id)

    public class Blog
        {     [Key]
            public int BId { 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; }
        public Blog BlogModel { get; set; }
      }

    3.无外键属性:如果未找到外键属性,则会引入名称为 <navigation property name><principal key property name> 或 <principal entity name><principal key property name> 在此示例中,隐藏外键是 BlogId 

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

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

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

    5.手动设置

    a.数据注解方式

    public int BlogForeignKey { get; set; } 
    
    [ForeignKey("BlogForeignKey")]  //设置外键
    public Blog Blog { get; set; }

    b.Fluent API 方式

    odelBuilder.Entity<Post>().HasOne(p => p.Blog).WithMany(b => 
    b.Posts).HasForeignKey(p => p.BlogForeignKey);
  • 相关阅读:
    VS2010/MFC编程入门之三十八(状态栏的使用详解)
    VS2010/MFC编程入门之三十七(工具栏:工具栏的创建、停靠与使用)
    VS2010/MFC编程入门之三十六(工具栏:工具栏资源及CToolBar类)
    VS2010/MFC编程入门之三十五(菜单:菜单及CMenu类的使用)
    VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
    VS2010/MFC编程入门之三十三(常用控件:标签控件Tab Control 下)
    走进JDK(十二)------TreeMap
    深入理解java虚拟机(二)-----垃圾回收
    深入理解java虚拟机(一)-----java内存区域以及内存溢出异常
    走进JDK(十一)------LinkedHashMap
  • 原文地址:https://www.cnblogs.com/netlock/p/15671359.html
Copyright © 2011-2022 走看看