zoukankan      html  css  js  c++  java
  • 笔记-实体关系配置

    显式外键配置
    referenceCollectionBuilder.HasForeignKey(p => p.BlogForeignKey);
    modelBuilder.Entity<Car>().HasKey(c => new { c.State, c.LicensePlate });
    referenceCollectionBuilder.HassForeignKey(s => new { s.CarState, s.CarLicensePlate })
    referenceCollectionBuilder.HasForeignKey("BlogId");
    显式主体键
    如果你想要引用主体实体主键之外的属性作为外键,可通过HasPrincipalKey 配置。
    modelBuilder.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts).HasForeignKey(p => p.BlogUrl).HasPrincipalKey(b => b.Url);
    modelBuilder.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts).HasForeignKey(p => new { p.BlogUrl, p.BlogState }).HasPrincipalKey(b => new { b.Url, b.State });
    必需和可选的关系
    可使用Fluent API 来配置关系为必需或可选,使用隐藏属性时非常有用。
    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 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; } }
    如果使用 Fluent API 配置此关系,则使用 HasOne 和 WithOne 方法。
    modelBuilder.Entity<Blog>().HasOne(p => p.BlogImage).WithOne(i => i.Blog).HasForeignKey<BlogImage>(b => b.BlogForeignKey);
    多对多关系
    关系型数据库中不支持多对多的映射,通过建立中间表连接,使用一对多的方式模拟多对多关 系。例如:用户角色表,学生选课表。
    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; }
    }
    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(t => new { t.PostId, t.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); } }

  • 相关阅读:
    opencv视屏流嵌入wxpython框架
    Linux下makefile学习
    关于pyinstall打包时的依赖问题
    python文件结构与import用法
    python3+dlib人脸识别及情绪分析
    慕课学习--DNS的作用
    力扣leetcode11. 盛最多水的容器
    力扣leetcode5.最长回文子串
    力扣leetcode1190. 反转每对括号间的子串
    基于Ubuntu1604+ROS-kinetic+roscpp的激光雷达定位算法从零开始移植
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/13527833.html
Copyright © 2011-2022 走看看