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");

  • 相关阅读:
    windows如何查看删除记录
    nodejs 服务器 崩溃 2种解决办法
    WINDOWS常用端口列表
    windows端口
    普通交换机不需要任何设置,也不能设置
    二层网管交换机应用——访问控制功能管理内网电脑上网行为
    使用 Easy Sysprep v4(ES4) 封装 Windows 7教程
    A电脑的gho还原到B电脑上的驱动解决方案
    servlet 容器与servlet
    依赖注入与控制反转
  • 原文地址:https://www.cnblogs.com/qingfenglin/p/13527822.html
Copyright © 2011-2022 走看看