为什么这个列在EF代码优先迁移中生成? [英] Why is this column getting generated in EF Code-First migrations?

查看:152
本文介绍了为什么这个列在EF代码优先迁移中生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [必需] 
public int MentorId {get;组; }
public virtual User Mentor {get;组;

[必需]
public int StudentId {get;组; }
public virtual User Student {get;组;

用户模型:

  public virtual ICollection< Mentorship>导师{get;组; 

流利的API:

  modelBuilder.Entity< Mentorship>()
.HasRequired(c => c.Mentor)
.WithMany()
.HasForeignKey(u => u .MentorId);

modelBuilder.Entity< Mentorship>()
.HasRequired(c => c.Student)
.WithMany()
.HasForeignKey(u => u.StudentId);

在我的数据库中,我看到已经正确填充的StudentId和MentorId列,但是我也看到没有被任何东西使用的User_UserId列。我做错了什么?

解决方案

你已经使用了 WithMany()重载,将关系配置为需要:许多没有导航属性在关系的另一边 - 但你有一个导航属性在另一边的关系。



尝试这样:

  modelBuilder.Entity< Mentorship>()
.HasRequired(c => c .Mentor)
.WithMany(d => d.Mentorships)
.HasForeignKey(u => u.MentorId);

modelBuilder.Entity< Mentorship>()
.HasRequired(c => c.Student)
.WithMany(d => d.Mentorships)
.HasForeignKey(u => u.StudentId); // oops!只是意识到我们只是
//指定导师正在使用MentorId
//作为FK

参考文献:



需要与许多方法



为什么要获得额外的外键?



修改
抓住这个。只是意识到你正在尝试创建两个关系,只有一个导航属性在很多方面。您不能有2个外键的导航属性。您需要在用户侧引入继承,或从用户中删除导师属性导航属性课程或引入单独的 StudentMentorships MentorMentorships 导航属性



编辑2
定义单独导航属性后查找用户

  int userId = 123; //我们要查找的
var user = Users.Where(x => x.StudentMentorships.Any(s => s.StudentID == userId)
|| x.MentorMentorships.Any(s => s.MentorID == userId);


I have a Mentorship entity, which has Student and Mentor as FKs:

    [Required]
    public int MentorId { get; set; }
    public virtual User Mentor { get; set; }

    [Required]
    public int StudentId { get; set; }
    public virtual User Student { get; set; }

User model:

    public virtual ICollection<Mentorship> Mentorships { get; set; }

Fluent API:

    modelBuilder.Entity<Mentorship>()
        .HasRequired(c => c.Mentor)
        .WithMany()
        .HasForeignKey(u => u.MentorId);

    modelBuilder.Entity<Mentorship>()
        .HasRequired(c => c.Student)
        .WithMany()
        .HasForeignKey(u => u.StudentId);

In my database, I see StudentId and MentorId columns which have been populated correctly, but I also see a User_UserId column that is not being used by anything. What have I done wrong?

解决方案

You have used the WithMany() overload that configures the relationship to be required:many without a navigation property on the other side of the relationship - but you do have a navigation property on the other side of the relationship.

Try this:

modelBuilder.Entity<Mentorship>()
    .HasRequired(c => c.Mentor)
    .WithMany(d => d.Mentorships)
    .HasForeignKey(u => u.MentorId);

modelBuilder.Entity<Mentorship>()
    .HasRequired(c => c.Student)
    .WithMany(d => d.Mentorships)
    .HasForeignKey(u => u.StudentId);//oops! just realised that we just 
                                     //specified that Mentorships is using MentorId 
                                     //as the FK

References:

Required WithMany Method

Why do I get an extra foreign key?

Edit Scratch that. Just realised you are trying to create two relationships with only one navigation property on the many side. You can't have a navigation property with 2 foreign keys. You need to introduce inheritance on the User side or remove the Mentorships navigation property from the User class or introduce separate StudentMentorships and MentorMentorships navigation properties

Edit 2 Finding a user once you've defined separate navigation properties

int userId = 123;//the one we want to find
var user = Users.Where(x => x.StudentMentorships.Any(s => s.StudentID == userId) 
                         || x.MentorMentorships.Any(s => s.MentorID == userId);

这篇关于为什么这个列在EF代码优先迁移中生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆