Justinonday
Justinonday

Reputation: 147

How can define multiple KEY in EntityFrameWorkClass C#

[Table("Table_UserImages")]
public class UserImage
{
     [Key, Column("UserID")]
     public Nullable<Guid> UserID { get; set; }
     [Key, Column("ImageID")]
     public Nullable<int> ImageID { get; set; }  
}

Both column are Primary key but model only accept one Key at a time,then how can i over come this? have any solution ? please share?

Upvotes: 0

Views: 1900

Answers (1)

Arjan Einbu
Arjan Einbu

Reputation: 13672

I believe you're describing a composite key, that is a key composed of two or more columns.

To describe this in EF, you need to also define the column ordering for the keys. Like this:

[Table("Table_UserImages")]
public class UserImage
{
    [Key, Column("UserID", Order=0)]
    public Guid? UserID { get; set; }
    [Key, Column("ImageID", Order=1)]
    public int? ImageID { get; set; }  
}

Upvotes: 4

Related Questions