对我来说简单但又神秘:为什么 StringPropertyConfiguration (以及所有其他 PropertyConfiguration)类有 2 个重载 IsConcurrencyToken()
?
第一个:
public StringPropertyConfiguration IsConcurrencyToken()
Configures the property to be used as an optimistic concurrency token.
第二个:
public StringPropertyConfiguration IsConcurrencyToken(bool?)
Configures whether or not the property is to be used as an optimistic concurrency token.
你为什么要用一个而不是另一个?在我看来,这两个重载之间没有任何区别(至少在使用它们时没有区别)...
通过使用第一个你会写这样的东西:
modelBuilder.Entity<Author>()
.Property(x => x.Name)
.IsConcurrencyToken();
然后使用第二个你会写:
modelBuilder.Entity<Author>()
.Property(x => x.Name)
.IsConcurrencyToken(true/false/null);
我错过了什么吗?
最佳答案
我的看法...
IsConcurrencyToken()
默认为 true 以提供一种简单、流畅的方式来定义实体。
IsConcurrencyToken(bool?)
允许作者明确设置实体的 ConcurrencyMode
。这对于高级场景很有用:
- 覆盖 POCO 上的
[ConcurrencyCheck]
属性 - 允许约定(在 EF 4.1 RTW 中删除)根据一些自定义约定启用/禁用
ConcurrencyMode
最后,我认为 IsConcurrencyToken(false)
比 IsNotConcurrencyToken()
好。
关于c# - EF 代码优先 - IsConcurrencyToken(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980128/