• 笔记-导航属性关系配置


    多导航属性
    在 Post 类中,可能需要跟踪是文章的创建者和最后编辑者,下面是 Post 类的两个新的导航属性。
    public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; }
    public User Author { 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; } }
    注意:同样可使用ForeignKeyAttribute 特性显式指定外键名。
    modelBuilder.Entity<Post>().HasOne(p => p.Author).WithMany(u=>u.AuthoredPosts).HasForeignKey("AuthorId");
    modelBuilder.Entity<Post>().HasOne(p => p.Contributor).WithMany(u => u.ContributedToPosts);
    若要使用Fluent API 配置关系,您首先确定构成关系的导航属性, 使用 HasOne 或 HasMany开始配置当前实体类 型上的导航属性,然后调用WithOne 或 WithMany 来配置反导航属性。 HasOne 和 WithOne 用于引用导航属 性,HasMany和 WithMany 用于集合导航属性。
    modelBuilder.Entity<Post>().HasOne(p => p.Blog).WithMany(b => b.Posts); modelBuilder.Entity<Blog>().HasMany(p => p.Posts).WithOne(b => b.Blog);
    单一导航属性
    modelBuilder.Entity<Blog>().HasMany(b => b.Posts).WithOne();
    其中:WithOne 不指定参数即可
    显式外键配置
    referenceCollectionBuilder.HasForeignKey(p => p.BlogForeignKey);
    modelBuilder.Entity<Car>().HasKey(c => new { c.State, c.LicensePlate });
    referenceCollectionBuilder.HasFoHasForeignKey(s => new { s.CarState, s.CarLicensePlate })
    referenceCollectionBuilder.HasForeignKey("BlogId");

  • 相关阅读:
    ES6-10笔记(class类)
    ES6-10笔记(let&const -- Array)
    小程序的表单提交
    小程序表单回显
    小程序template模板的使用和模板多数据传递
    微信小程序的初始配置
    babel 版本原因运行报错,解决办法
    webpack 和 webpack-cli 安装和使用中出现的问题
    jQuery中操作属性的方法attr与prop的区别
    javaScript 添加和移除class类名的几种方法
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/13527822.html
Copyright © 2020-2023  润新知