Does Entity Framework 5 support unique constraints?

后端 未结 5 559
日久生厌
日久生厌 2020-12-10 01:49

Wondering if Entity Framework 5 supports unique constraints on entity properties? If so, how can I specify that a property should be unique?

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 02:33

    Well i was looking for solution and finally i found it. when you generate migration in code you can create unique key

     CreateTable(
                    "dbo.TaBLE",
                    c => new
                        {
                            Id = c.Int(nullable: false, identity: true),
                            Date = c.DateTime(nullable: false),
                            Meter_Id = c.Int(),
                        })
                    .PrimaryKey(t => t.Id)
    
                    .Index(t => new {t.Meter_Id, t.Date}, true);
    

    Validation before insert you can do on BLL level, so i think it can solve your problem.

提交回复
热议问题