POCO Class in EF not working as Expected

给你一囗甜甜゛ 提交于 2019-12-05 07:24:47

First, to understand your problem, what you need to know is that the EDMX file is just an XML file that contains 3 different sections:

  • CSDL: Conceptual schema definition language
  • SSDL: Store schema definition language
  • MSL: Mapping specification language

The CSDL contains the entities and relationships that make up your conceptual model. The SSDL describes your DB model and the MSL is the mapping between the 2.

The “Update Model From DB” process will update the SSDL (change everything that is inconsistent with the current DB schema), it will only modify the CSDL in case you’ve added new things to your DB schema.

This is quite a normal behavior since your Conceptual schema may/should differ from your DB schema (unless you want your Domain model to look exactly like a DB model which obviously do not sound as OOP/DDD best practices).

As for @Peru, the solution would be to delete the concerned entity (not the entire EDMX!) and then perform the “Update Model From DB” process.

Hope this helps!

Edit:

There's a tool, not for free, which is a Visual Studio plugin that allows you apply changes made into the DB, both on the CSDL and SSDL files: Huagati DBML/EDMX Tools. The only "free" solution is deleting the entity (or the right field within this entity) that needs to be updated.

Remember that CSDL is supposed to be maintained by developers and must look like an Object Model rather than a DB Model. Imagine you have setup inheritance between your entities or that you have split 1 DB table to 2 EDMX entities, running "Update model from DB" should not overwrite everything!

Personally, I'd open the edmx file as XML and find the problem node and delete it. The file is pretty easy to understand - don't be scared to at least try it out.

Only the manual edit worked for a View in my project. (going from smallint to decimal(18,2) )

Open the .EDMX file in a text editor, find appropite section, and manually change the Property Type="..." value.

I know I'm a bit late here but there is a relatively simple way to generate your POCOs/update your DbContext from the EDMX designer.

Go to your designer, right click on the empty area, click "Update Model from Database", add/update your tables. This will update your edmx XML. After you've verified that your table has been added to the CSDL, SSDL, and MSL sections, right click on your T4 template in the solution explorer (<Name>.tt, not <Name>.Context.tt) and click "Run Custom Tool" - this will generate POCOs for any updated objects. Note that if you are creating new entities, they will not be added to your DbContext class, you will still have to do this manually by adding the following line at the bottom of your class: public virtual DbSet<Entity_Name_Goes_Here> EntityNames { get; set; }

Once you update the fields in Db, Find the respective model.cs file then delete those fields from the model. now Update the EDMX file (Update Model from Database). It worked for me.

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