zoukankan      html  css  js  c++  java
  • 笔记-导航属性关系配置

    多导航属性
    在 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");

  • 相关阅读:
    v-for给img的src动态赋值问题
    关于vue+axios上传文件的踩坑分析
    关于nth-of-type和nth-child的关系
    关于fetch
    关于移动端适配
    golang变量作用域问题-避免使用全局变量
    grpc-gateway:grpc转换为http协议对外提供服务
    google的grpc在golang中的使用
    golang中的rpc包用法
    homebrew常用命令
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/13527822.html
Copyright © 2011-2022 走看看