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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14612813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:25:34  来源:igfitidea点击:

Entity Framework Code First Using One column as Primary Key and another as Auto Increment Column

c#entity-frameworkef-code-first

提问by Seevali H Rathnayake

I have a class named Sale

我有一个名为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 Idas the Auto Incrementcolumn and the TrNoas the Primary Keycolumn.

在数据库中,我想要Id作为Auto Increment列和TrNo作为Primary Key列。

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

请先告诉我如何使用 EF5 代码执行此操作。

Thanks.

谢谢。

采纳答案by Seevali H Rathnayake

Apparently the answerof @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.

显然@IronMan84的答案是正确的。但它对我不起作用。我稍微修改了它以应用我的另一个条件。它奏效了。我什么也没做。

This is my solution.

这是我的解决方案。

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

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

不幸的是,我无法将@IronMan84 的答案作为正确的答案,因为它对我不起作用。

回答by Jake

I believe you can do this using Fluent API

我相信你可以使用 Fluent API 做到这一点

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Sale>().Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    modelBuilder.Entity<Sale>().Property(a => a.TrNo).HasKey(b => b.TrNo);
}

回答by Corey Adler

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

回答by Jephren Naicker

This helped me. Hope this helps anyone else that still looking around

这对我有帮助。希望这可以帮助其他仍在环顾四周的人

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

        [Key]//set as Primary key
        [DatabaseGenerated(DatabaseGeneratedOption.None)]// switch off autogenerated PK
        public string TrNo { get; set; }

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

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