Please read that question: Create code first, many to many, with additional fields in association table
Here is the scenario from OP:
public class Member
{
public int MemberID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int CommentID { get; set; }
public string Message { get; set; }
public virtual ICollection<Member> Members { get; set; }
}
public class MemberComment
{
public int MemberID { get; set; }
public int CommentID { get; set; }
public int Something { get; set; }
public string SomethingElse { get; set; }
}
The accepted answer was:
public class MemberComment
{
[Key, Column(Order = 0)]
public int MemberID { get; set; }
[Key, Column(Order = 1)]
public int CommentID { get; set; }
public virtual Member Member { get; set; }
public virtual Comment Comment { get; set; }
public int Something { get; set; }
public string SomethingElse { get; set; }
}
Right now, my question is: What if I want add another identity field like a Guid or AutoInc int, by example:
public int MemberCommentID { get; set; }
I'm not in discussing about it's the better method or no, I want know how I do it?
I was thinking something like this
public class MemberComment
{
[Key, Column(Order = 0)]
public int MemberCommentID { get; set; }
[Key, Column(Order = 1)]
public int MemberID { get; set; }
[Key, Column(Order = 2)]
public int CommentID { get; set; }
public virtual Member Member { get; set; }
public virtual Comment Comment { get; set; }
public int Something { get; set; }
public string SomethingElse { get; set; }
}
But then EF will (I think) use the 3 fields in the key, when the right is MemberCommentID is alone 1 key, and MemberID+CommentID is another key.