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

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

问题描述

我有一个名为销售

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

而在数据库中,我想要 Id 作为自动增量列和 TrNo 作为主密钥列。

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

请告诉我如何使用EF5代码。

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

谢谢。

推荐答案

显然,答案。但它对我来说并不奏效我略微修改它来应用我的另一个条件。它工作。我没有做任何其他事情。

Apparently the answer of @IronMan84 correct. But it didn't work for me. I slightly modified it to apply my another condition. And it worked. I did nothing else.

这是我的解决方案。

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

    [Key, Column(TypeName = "varchar"), MaxLength(50)]
    public string TrNo { get; set; }

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

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

不幸的是,我不能把@ IronMan84的答案当作正确的一个因为它不适合我。

Unfortunately I can't make the answer of @IronMan84 as the correct one as it didn't work for me.

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

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