且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架代码首先使用一列作为主键,另一为自动递增列

更新时间:2023-02-05 18:33:11

您也可以用数据注释做到这一点:

 公共类售
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)
公众诠释标识{搞定;组; }

[关键]
公共字符串TrNo {搞定;组; }

公众的DateTime日期{搞定;组; }
公众诠释客户ID {搞定;组; }

公众的ObservableCollection< SaleDetail> SaleDetails {搞定;组; }
}


I have a class named Sale

public class Sale
{
    public int Id { get; set; }
    public string TrNo { get; set; }
    public DateTime Date { get; set; }
    public int CustomerID { get; set; }

    public ObservableCollection<SaleDetail> SaleDetails { get; set; }
}

And in the database, I want the Id as the Auto Increment column and the TrNo as the Primary Key column.

Please tell me how to do this using EF5 code first.

Thanks.

You can also do this with Data Annotations:

public class Sale
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Key]
    public string TrNo { get; set; }

    public DateTime Date { get; set; }
    public int CustomerID { get; set; }

    public ObservableCollection<SaleDetail> SaleDetails { get; set; }
}