【发布时间】:2019-11-19 07:30:29
【问题描述】:
我有一个简短的问题,正在寻找最好的方法来做到这一点,不知道 EF 是否有能力?我正在使用 EntityFramework 6.3。
我有以下父子场景,
public class Application{
[Key]
public int ApplicationId {get;set;}
public string Name {get;set;}
public string Status {get;set;}
public virtual List<Document> Documents {get;set;}
}
public class Document{
[Key]
public int DocumentId {get;set;}
[Index("IX_ApplicationDocument", 1, IsUnique = true)]
public string DocumentType {get;set;}
[Index("IX_ApplicationDocument", 1, IsUnique = true)]
public string Name {get;set;}
public int ApplicationId {get;set;}
[ForeignKey("ApplicationId")]
public Application Application {get;set;}
}
所以向一个部门提出申请,并存储在数据库中,每个申请都有一个状态,提交时,状态为待处理,因为在批准之前必须进行各种验证。当申请被拒绝时,提交者必须提出新的申请(请注意,我使用了一个比实际情况更简单的示例),但是,申请人可以再次提交相同的文件。问题是,这已经存在于系统中,无法复制。如您所见,他们第二次尝试提交时会抛出约束异常。如何使用 EF 克服这个问题,有没有办法根据父母的状态创建约束,或者这只能以编程方式完成?
【问题讨论】:
-
你能澄清一下吗,你希望 documentId 在未来是唯一的,但对现有的来说可以重复吗?
-
基本上,如果您想避免新条目重复,则可以使用实体框架在 SQL Server 上创建过滤的唯一索引。如果是这种情况,请告诉我
标签: c# entity-framework-6 data-annotations entity-relationship unique-constraint