开发者

How can define multiple KEY in EntityFrameWorkClass C#

开发者 https://www.devze.com 2023-04-01 06:05 出处:网络
[Table(\"Table_UserImages\")] public class UserImage { [Key, Column(\"UserID\")] public Nullable<Guid> UserID { get; set; }
[Table("Table_UserImages")]
public class UserImage
{
     [Key, Column("UserID")]
     public Nullable<Guid> UserID { get; set; }
     [Key, Column("ImageID")]
     public Nullable<int> ImageID { get; set; }  
}

开发者_Go百科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?


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; }  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消