实体框架代码首先使用一列作为主键,另一为自动递增列 [英] Entity Framework Code First Using One column as Primary Key and another as Auto Increment Column

查看:132
本文介绍了实体框架代码首先使用一列作为主键,另一为自动递增列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为出售



 公共类售
{$ B类$ b公众诠释标识{搞定;组; }
公共字符串TrNo {搞定;组; }
公众的DateTime日期{搞定;组; }
公众诠释客户ID {搞定;组; }

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

和数据库中的,我想在标识自动递增列和 TrNo 主关键列。



请告诉我如何先做到这一点使用EF5代码。



< 。p>感谢


解决方案

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

 公共类售
{
[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; }
}

这篇关于实体框架代码首先使用一列作为主键,另一为自动递增列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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