POCO Class in EF not working as Expected

user2067567 picture user2067567 · Mar 21, 2013 · Viewed 17.7k times · Source

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?

Answer

MaxSC picture MaxSC · Apr 25, 2013

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!