为什么我得到一个额外的外键列与实体框架代码第一外键属性? [英] Why am I getting an extra foreign key column with Entity Framework Code First Foreign Key Attributes?

查看:85
本文介绍了为什么我得到一个额外的外键列与实体框架代码第一外键属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的课程看起来像这样

  public class Status 
{
[Key]
public int StatusID {get;组; }
public string Name {get;组; }
public int MemberID {get;组; }

[ForeignKey(MemberID)]
public virtual Member Member {get;组; }

public int PosterID {get;组; }

[ForeignKey(PosterID)]
public virtual Member Poster {get;组; }

public virtual ICollection< StatusLike> StatusLikes {get;组; }
public virtual ICollection< StatusComment> StatusComments {get;组;
}

我的会员类看起来像这样

  public class Member 
{
[Key]
public int MemberID {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string Bio {get;组; }

public virtual ICollection< MemberCourseTaken>会员中心组; }
public virtual ICollection< Status>状态{get;组; }
public virtual ICollection< Club> FoundedClubs {get;组; }

public string EmailAddress {get;组; }
public string Password {get;组; }
public string Phone {get;组; }

public int AccountSourceID {get;组; }
public AccountSource AccountSource {get;组; }

public int AddressID {get;组; }
public Address Address {get;组; }
public string ProfilePhoto {get;组; }

public int MemberRankID {get;组; }
public MemberRank MemberRank {get;组; }
public DateTime Created {get;组; }
public DateTime Modified {get;组; }
}

由于任何原因,创建的数据库表具有以下列

  StatusID 
名称
MemberID
PosterID
Member_MemberID

MemberID PosterID Member_MemberID 是外键。



如何保持 Member_MemberID 被生成

解决方案

您的 Member_MemberID 由于 Member.Statuses 属性,因此创建了列。我可以想象,这不是你想要的。可能的成员和状态应该彼此独立存在,所以你需要一个连接表。



我不知道你是否已经使用了 OnModelCreating 覆盖DbContext,但这是更改成员和状态之间的映射的地方:

  protected覆盖void OnModelCreating(DbModelBuilder mb)
{
mb.Entity< Member>()。HasMany(m => m.Statuses).WithMany();
}

这将创建一个具有两个Id列作为外键的表MemberStatuses表。这是一种在关联的其他一侧没有导航属性来建立多对多关系的方法。 (我不认为你想在状态中的成员属性。


I recently came across this strange problem with Entity Framework Code First.

My class looks like this

public class Status
{
        [Key]
        public int StatusID { get; set; }     
        public string Name { get; set; }
        public int MemberID { get; set; }

        [ForeignKey("MemberID")]
        public virtual Member Member { get; set; }                

        public int PosterID { get; set; }

        [ForeignKey("PosterID")]
        public virtual Member Poster { get; set; }        

        public virtual ICollection<StatusLike> StatusLikes { get; set; }        
        public virtual ICollection<StatusComment> StatusComments { get; set; }
}

My Member class looks like this

 public class Member
    {
        [Key]
        public int MemberID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Bio { get; set; }

        public virtual ICollection<MemberCourseTaken> MemberCourseTakens { get; set; }
        public virtual ICollection<Status> Statuses { get; set; }
        public virtual ICollection<Club> FoundedClubs { get; set; }

        public string EmailAddress { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }

        public int AccountSourceID { get; set; }
        public AccountSource AccountSource { get; set; }

        public int AddressID { get; set; }
        public Address Address { get; set; }
        public string ProfilePhoto { get; set; }

        public int MemberRankID { get; set; }
        public MemberRank MemberRank { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }
    }

And for whatever reason the database table that is created has the following columns

StatusID
Name
MemberID
PosterID
Member_MemberID

with MemberID, PosterID, and Member_MemberID being foreign keys.

How can I keep Member_MemberID from being generated?

解决方案

Your Member_MemberID column is created because of the Member.Statuses property. I can imagine that this is not what you want. Probably members and statuses should exist independent of each other, so you need a junction table.

I don't know if you already use the OnModelCreating override of the DbContext, but that's the place to change the mapping between Member and Status:

protected override void OnModelCreating(DbModelBuilder mb)
{
    mb.Entity<Member>().HasMany(m => m.Statuses).WithMany();
}

This will create a table MemberStatuses table with the two Id columns as foreign keys. This is a way to model a many-to-many relationship without a navigation property on the "other" side of the association. (I don't think you want a Members property in Status).

这篇关于为什么我得到一个额外的外键列与实体框架代码第一外键属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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