使用 Entity Framework 的唯一主键
Unique primary key using Entity Framework
我是 EF 新手。假设我在数据库中有一个 table 这样的:
ID FirstName LastName DateOfBirth
-------------------------------------------
1 John Smith 1.1.1990
2 Mary Wilson 5.1.1991
现在我使用 EF:
将新实体插入 table
dbcontext.Persons.Add(new Person
{
FirstName = "John",
LastName = "Smith",
DateOfBith = "1.1.1990"
});
dbcontext.SaveChanges();
我需要代码来抛出异常,因为该行已存在于数据库中,但 EF 所做的是将 ID 列递增 1 并创建新记录:
ID FirstName LastName DateOfBirth
--------------------------------------------
1 John Smith 1.1.1990
2 Mary Wilson 5.1.1991
3 John Smith 1.1.1990
EF 能做到这一点吗?
对于 EF,您需要执行如下操作:
[Index("IX_UniqueConstraint", 1, IsUnique = true)]
public string FirstName { get; set; }
[Index("IX_UniqueConstraint", 2, IsUnique = true)]
public string LastName { get; set; }
[Index("IX_UniqueConstraint", 3, IsUnique = true)]
public DateTime DateOfBirth { get; set; }
这将对 3 列设置唯一约束。
您已经将 ID
列定义为 identity column and it has been considered as your primary key and will be increased by one any time you insert a new record in your table. This is why you are allowed to insert a duplicate entity. You need to specify which column needs to be declared as PK, either in your model if you are using code-first approach and by using Data Annotation,如下所示:
[Key]
public string FirstName { get; set; }
或者使用唯一约束:
[Index("IX_UniqueConstraint", 1, IsUnique = true)]
public string FirstName { get; set; }
[Index("IX_UniqueConstraint", 2, IsUnique = true)]
public string LastName { get; set; }
[Index("IX_UniqueConstraint", 3, IsUnique = true)]
public DateTime DateOfBirth { get; set; }
您也可以使用 fluent API 来达到这个目的:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Persons>().HasKey(c => new { c.FirstName, c.LastName, c.DateOfBirth });
}
或者,如果您使用的是 DB-first 方法,则可以在您的数据库中声明它。
我是 EF 新手。假设我在数据库中有一个 table 这样的:
ID FirstName LastName DateOfBirth
-------------------------------------------
1 John Smith 1.1.1990
2 Mary Wilson 5.1.1991
现在我使用 EF:
将新实体插入 tabledbcontext.Persons.Add(new Person
{
FirstName = "John",
LastName = "Smith",
DateOfBith = "1.1.1990"
});
dbcontext.SaveChanges();
我需要代码来抛出异常,因为该行已存在于数据库中,但 EF 所做的是将 ID 列递增 1 并创建新记录:
ID FirstName LastName DateOfBirth
--------------------------------------------
1 John Smith 1.1.1990
2 Mary Wilson 5.1.1991
3 John Smith 1.1.1990
EF 能做到这一点吗?
对于 EF,您需要执行如下操作:
[Index("IX_UniqueConstraint", 1, IsUnique = true)]
public string FirstName { get; set; }
[Index("IX_UniqueConstraint", 2, IsUnique = true)]
public string LastName { get; set; }
[Index("IX_UniqueConstraint", 3, IsUnique = true)]
public DateTime DateOfBirth { get; set; }
这将对 3 列设置唯一约束。
您已经将 ID
列定义为 identity column and it has been considered as your primary key and will be increased by one any time you insert a new record in your table. This is why you are allowed to insert a duplicate entity. You need to specify which column needs to be declared as PK, either in your model if you are using code-first approach and by using Data Annotation,如下所示:
[Key]
public string FirstName { get; set; }
或者使用唯一约束:
[Index("IX_UniqueConstraint", 1, IsUnique = true)]
public string FirstName { get; set; }
[Index("IX_UniqueConstraint", 2, IsUnique = true)]
public string LastName { get; set; }
[Index("IX_UniqueConstraint", 3, IsUnique = true)]
public DateTime DateOfBirth { get; set; }
您也可以使用 fluent API 来达到这个目的:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Persons>().HasKey(c => new { c.FirstName, c.LastName, c.DateOfBirth });
}
或者,如果您使用的是 DB-first 方法,则可以在您的数据库中声明它。