How to make composite unique key in ASP.NET Boilerplate?

*爱你&永不变心* 提交于 2019-12-02 09:54:33

Try this:

public class SomeClass : Entity<int>
{
    [Column("Code")]
    public override int Id {get; set;}

    public virtual string Name { get; set; }    
}

[DatabaseGenerated(DatabaseGeneratedOption.None)] will make the Database to NOT create the Code column. What you need in reality is to override the Id and rename it to Code.

And to have a composite key, simply add

[Key]
public virtual int Value { get; set; }

field.

Adding solution for others.

Don't modify anything just add the below line

modelBuilder.Entity<Test>(b =>
        {
            b.HasIndex(e => new { e.Code, e.Value}).IsUnique();
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!