Use Entity Framework POCO resolve the default value (Default Value) written questions v4

Use Entity Framework POCO resolve the default value of the (Default Value) written questions


After six hours of practice and verification, the original article po out of practice, only in the specific project and brother UT can perform (the reason is not clear)

After the brother Rebuild the same programs and databases can not reproduce the same effect.

In addition, although many articles in CSDL reaction is an update, but the update is not SSDL Bug.

But the younger brother import StoreGeneratedPattern = Computed, if found SSDL also changed Computed words!

EF will ignore the value of this field is written, so then they have to update its value is not OK. 

This is no way to accept certain fields, so there is no change, then SSDL! Still relatively no problem.

At least in the initial value can be added DTO object to avoid or resolve when Insert syntax.

When you manually change the SSDL, so long as re-pull Model or updated, the SSDL will be updated to None.

PS .. Identity and TimeStamp currently identified patterns of these two fields will be provided to Computed SSDL

Therefore, the current default value of EF There are two approaches

  1. AP will not read and write, they do not add to the EF Model, you can insert solve the problem (that is, do not appear in the CSDL)
  2. AP will read and write, please add default values ​​directly on the POCO. (If you can use Nullable Or type?)

L until when asked found that there is a problem like this

(Thanks to the assistance R)

 

The problem is that if the database has a "default values" of the situation, EF is no way to write

 

L solution was found only on two, no matter what kind of very inconvenient

First, rewrite SSDL (EDMX file) (to do this as long as a database synchronization, just change will be gone)

Second, the value of the field is always filled into (the program does not plug into the value is called the default value)

 

The question in the discussion boards have appeared in a workaround way

 

In fact, there is another approach can solve this problem! ! !

 

 

Beginning brother did not notice probably because, younger brother for the first time has been rewritten to POCO types of relationships

There had previously po EF 4 POCO rewritten and automatically generate POCO objects of the article, interested can refer to

 

---------- Please pay special attention! ----------------

v2

Custom Tool "must" If empty Oh! Otherwise, Entity Framework POCO will not integrate

Or wait for updates EDMX time or no effect! !

image  

-------------------------------------------------

 

In order to prove to be true (there are pictures and the truth)

So we build from DB started! !

 

image  
DB DB Project change of course from the start, all the changes have been incorporated into version control, usually the default values are not to null.
And in order to verify the default value of a plurality of different, here specifically added and TimeStamp DateTime

 

image
To establish a default condition!

 

image

image  
Because, TimeStamp type itself will have been worth it, so here just to make up the CreateDate like! 

 

We are changing for the better after a synchronize database to test it! !

 

Tip: If you want the data can be in accordance with the new, the latest modifications will have time to record words. TimeStamp recommended direct use of it, then do not need another program to maintain its newest value. It can save a very, very many workers

 

image
EDMX direct start and synchronize to the copy of the latest model

image   

  image
确认一下 CreateDate 的属性,的确 StoreGeneratedPattern 是 Computed ! ( 先声明,我没有去改 SSDL )

那~~ 另外一个字段 TimeStamp 去那了? 因为 TimeStamp 我的程序都不会去动到,所以刚刚我先删除了。

 

 

---------------------------------------------------

v3

现在我重新建置却一直都是 None 可能是因为当时不小心点到了吧!很囧

不过,重新再进行验证后,其实发现 StoreGeneratedPattern 有没有设定都没关系!

( 之前都有先入为主的观念 Orz )

目前知道会自动产生StoreGeneratedPattern 只有 Identity 和 TimeStamp 两种格式

 

v2

若是原来就已经有这个字段的话!那么 StoreGeneratedPattern 就会用原来旧的

请重新建立 或是 手动改成正确的就行了!

---------------------------------------------------

 

image  
因为模型上已经有了,所以 DTO 对象也要加上该对应字段。不然会无法正常执行 
 

( 到目前为止只要加上这一行 code 就好,其他的都先不用动。 )

 

改好后就要验证,刚刚新增的字段是否会对“即有”程序造成影响

当然用单元测试来跑,马上就可以确认了。 (  快速又简单 )

 

image  
因为是默认值验证,所以就直接拿原本的 UnitTest 连改都不需要
可以看到测试中只有用到 .Name 的属性 (字段) 

 

image  
太棒了!很快地~~ 测试通过了。

 

  image
因为这段程序会自动产生三个默认值,所以也一并确认数据库的内容,都可以看到 TimeStamp 和 CreateDate 都有默认值。

v3:为了有图有真像,还是稍微把过程中断将图抓出来。

 

 

So to prove when combined Entity Framework 4 + POCO, the problem is that the default values ​​will not be affected.

And after no longer need to worry about database synchronization, modified SSDL will be changed back.

Original: Large columns  using the Entity Framework POCO resolve the default value of the (Default Value) written questions v4


Guess you like

Origin www.cnblogs.com/chinatrump/p/11496669.html