POCO Class in EF not working as Expected

19,150

Solution 1

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!

Solution 2

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.

Solution 3

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; }

Solution 4

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.

Share:
19,150
user2067567
Author by

user2067567

Updated on June 04, 2022

Comments

  • user2067567
    user2067567 almost 2 years

    I have created a DataBase in SQL and created an EDMX in Visual Studio 2012. It automatically created POCO (TT) classes. Everything looks fine.

    Now I change the column name of a table. I update the EDMX. Opening the EDMX in XML and everything looks fine.

    Question 1

    After I ran Custom tool in TT, I see that a new property got created additionally, e.g.:

    SQL table name : Student
    
    Column name : sName
    

    In my POCO Class

    public int sName{ get; set; }
    

    got automatically created.

    Now I change the column name in SQL to

    Column name : studentName
    

    My POCO class

    public int sName{ get; set; }
    
    public int studentName{ get; set; }
    

    Is this a bug or do I need to do something to fix this?

    What should I do to avoid this?

    Question 2

    Also, if I change the data type of any SQL column and update the model from the DB in my EDMX designer, the Conceptual Model is not updated. How do I go about this?