美文网首页
2020-03-31 16:00:00 InverseProve

2020-03-31 16:00:00 InverseProve

作者: daiwei_9b9c | 来源:发表于2020-05-21 20:38 被阅读0次

提纲

InverseProperty 标注的用途

用于在某一个导航属性上标注其在相关实体上的反向导航属性,构造函数必须指定相关实体上的反向导航属性,且此属性的类型必须和导航属性的反向导航属性一致.

先看不带有 InversePropery 的例子,但是在 EFCore中无法运行

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 int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<Post> AuthoredPosts { get; set; }
    public List<Post> ContributedToPosts { get; set; }
}

这个例子在 EFCORE中无法创建表.
错误原因,
Unable to determine the relationship represented by navigation property 'User.AuthoredPosts' of type 'List<Post>'.
不能查明到由导航属性 User.AuthoredPosts (类型, List<Post>) 所表示的关系
这个 determine 有 查明/探测/决定的意思. 我们这儿就翻译作 查明 吧.

但是在 EF中,这个可以创建表,只是在 Post表中会有4个外键,
因为 EF找到了 4个关系,

  • Post 中的 Author 和 Contributor
  • User 中的 AuthoredPosts 和 ContributedToPosts
    而EF 不知道这些关系其实是2对导航-反向导航的关系 ,所以采取默认的处理措施,
    即给 每一个关系都会在数据库的相关实体中(也就是 Post 表中) 中创建一个外键.
    看到这儿就很简单了,

InverseProperty 标注用来标注某一个导航属性在相关实体中的另外一端的反向导航.

所以,它带有一个构造函数,也就是相关实体的导航属性的名称.
稍微改一下代码. 测试InverseProperty指向一个 int 类型的字段,想用这个字段作为外键.

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    [InverseProperty(nameof(User.AuthoredPosts)]
    public User Author { get; set; }
    public User Contributor { get; set; }

    public int OtherContributorId  { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public List<Post> AuthoredPosts { get; set; }

    [InverseProperty(nameof(Post.OtherContributorId)]
    public List<Post> ContributedToPosts { get; set; }
}

以上代码也无法执行,EFCORE会判断反向导航的属性是否和导航属性的类型是否匹配.
只是报了一个奇怪的错误: System.ArgumentNullException: Value cannot be null. (Parameter 'type')

正确写法如下:

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    [InverseProperty(nameof(User.AuthoredPosts)]
    public User Author { get; set; }

    public User Contributor { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Post> AuthoredPosts { get; set; }

    [InverseProperty(nameof(Post.Contributor)]
    public List<Post> ContributedToPosts { get; set; }
}

至少,比起 EF 来, EFCorer 显得更加智能一点...

相关文章

  • 2020-03-31 16:00:00 InverseProve

    提纲 InverseProperty 标注的用途 用于在某一个导航属性上标注其在相关实体上的反向导航属性,构造函数...

  • 2018-08-14

    9:00—10:00 梳理重点岗位需求,明确重点招聘岗位。 10:00—16:00 面试 16:00—18:00 ...

  • 2018-08-13

    9:00—10:00 梳理重点岗位需求,明确重点招聘岗位。 10:00—16:00 面试 16:00—18:00 ...

  • 今天2021-04-29我在干啥

    今天 8:00–10:00上课 14:00–16:00监考 16:30—15:30上课

  • 今天学习总结

    上午09:00-12:00 下午15:00-16:00 12:00-13:00午休,下象棋 16:00-17:10...

  • 财大商学院项目实践

    23日安排 10:00—12:00接机 12:00—14:00吃饭、休息 14:00—16:00参观公司 16:0...

  • 唯岁月可回首

    8:00~10: 00六级 10:00~11:10 文献 14:00~16:00上课 16:40~17:20 ...

  • 番茄工作法6.21

    12:30~13:00去宝龙路上 13:00~15:00收资源8个 15:00~16:00吃饭休息 16:00~2...

  • 9.12 运动记录

    运动时间:9月12日 14:00——16:00(上课:14:00—15:00;有氧+拉伸放松:15:00—16:0...

  • 00:00-00:00

    在一天又一天的等待着, 我没有绝对的伤悲。 只有相对而己的心, 这是一天里的故事会里。 在一时又一时的流淌着, 我...

网友评论

      本文标题:2020-03-31 16:00:00 InverseProve

      本文链接:https://www.haomeiwen.com/subject/xjtguhtx.html