EF 代码优先 - IsConcurrencyToken()
简单,但对我来说却很神秘:为什么StringPropertyConfiguration(以及所有其他 PropertyConfiguration)类有 2 个重载IsConcurrencyToken()
?
第一个:
public StringPropertyConfiguration IsConcurrencyToken()
配置要用作的属性 乐观并发令牌。
第二个:
public StringPropertyConfiguration IsConcurrencyToken(bool?)
配置属性是否 被用作乐观 并发令牌。
为什么你会使用其中一种而不是另一种?在我看来,这两个重载之间没有任何区别(至少在使用它们时没有区别)...
通过使用第一个重载,您会写出类似这样的内容:
modelBuilder.Entity<Author>()
.Property(x => x.Name)
.IsConcurrencyToken();
通过使用第二个重载,您会写出:
modelBuilder.Entity<Author>()
.Property(x => x.Name)
.IsConcurrencyToken(true/false/null);
我错过了什么吗?
Simple, but yet mysterious for me: Why do StringPropertyConfiguration (and all the other PropertyConfiguration) class(es) have 2 overloads for IsConcurrencyToken()
?
The first:
public StringPropertyConfiguration IsConcurrencyToken()
Configures the property to be used as
an optimistic concurrency token.
And the second:
public StringPropertyConfiguration IsConcurrencyToken(bool?)
Configures whether or not the property
is to be used as an optimistic
concurrency token.
Why would you use one over the other? As I see it, there's no difference at all between those two overloads (atleast not when working with them)...
By using the first you would write something like:
modelBuilder.Entity<Author>()
.Property(x => x.Name)
.IsConcurrencyToken();
And by using the second you would write:
modelBuilder.Entity<Author>()
.Property(x => x.Name)
.IsConcurrencyToken(true/false/null);
Have I missed something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的观点...
IsConcurrencyToken()
默认为 true,以提供一种简单、流畅的方式来定义实体。IsConcurrencyToken(bool?)
允许作者明确设置实体的ConcurrencyMode
。这对于高级场景非常有用:[ConcurrencyCheck]
属性ConcurrencyMode
约定最后,我认为
IsConcurrencyToken(false)
比IsNotConcurrencyToken()
更好。My opinion...
The
IsConcurrencyToken()
defaults to true to provide a simple, fluent manner to define the entity.The
IsConcurrencyToken(bool?)
allows the author to definitively set theConcurrencyMode
of the entity. This is useful for advanced scenarios:[ConcurrencyCheck]
attribute on the POCOConcurrencyMode
based on some custom conventionFinally, I think
IsConcurrencyToken(false)
is better thanIsNotConcurrencyToken()
.