EF Code First - IsConcurrencyToken()

喜你入骨 提交于 2019-12-10 17:06:48

问题


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?


回答1:


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 the ConcurrencyMode of the entity. This is useful for advanced scenarios:

  • Overriding the [ConcurrencyCheck] attribute on the POCO
  • Allowing a convention (removed in EF 4.1 RTW) to enable/disable the ConcurrencyMode based on some custom convention

Finally, I think IsConcurrencyToken(false) is better than IsNotConcurrencyToken().



来源:https://stackoverflow.com/questions/5980128/ef-code-first-isconcurrencytoken

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!