2

I'm pretty new to Entity Framework: I started from a database-first model to maintain an application created using a strange mixture of EF and plain old SQL.

I created my own fresh DB-first model and I'm fine with it. Today my boss asked me to add a new entity. Lack of foreign keys simplifies the scenario.

I have created my new entity in the diagram (it's made of three instances of a Complex Entity I just created) but now I have to make an incremental DB script to create the new table. I'm supposed to do that both for MySQL and SQL Server but let's start with the second.

So now I see that I have a compilation problem "No mapping for entity Entity" and if I use "Update model from database" command I see no option for pushing changes to DB, but that sounds correct given the word "from".

OK, I have tried to click "Table Mapping" from the right-click menu and I found the option to map the entity to the table. I was going to type the new table name in the "Add table or view" field and... WAIT! I can only select existing tables

I understand it's just for a single table so I can simply "Generate database from model" in order to get the full SQL script, find the table I want, run that to DB and "Update model from DB" so EF will see the table, BUT

I would like to understand how to create incremental scripts with Entity Framework. That is my question.

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
  • 1
    From https://msdn.microsoft.com/en-us/data/jj205424: " If you need to publish changes to a database that has already been deployed, you will need to edit the script or use a schema compare tool to calculate a migration script." – usr-local-ΕΨΗΕΛΩΝ Jan 30 '15 at 16:30

1 Answers1

8

You indicate you have a database-first design but appear to be working from a code-first mindset.

In database-first design the entity model is subordinate the underlying datastore. Changes to the model (or at least changes to the model which also require changes to the underlying datastore) occur FIRST on the database.

So how do you create a new table for the entity? You create the new table in your database (CREATE TABLE ...). Then using the "Update Model From Database" wizard you select the new table from the "Add" tab. EF will create the corresponding EF class automatically. If you already manually created the entity you should delete it otherwise you could end up with some weird entity naming (i.e. Customer1).

Database first does not have the capability to support table creation at the entity layer. Changes to the database are always one way, from the database to the entity model, hence the term "database first".

On the other hand if you are more comfortable creating entities directly and want to build a database from a set of entities you should be looking to create a "Code-First" design. Despite the name "code first" it is possible to get an initial set of entity classes from an existing database. The term "code first" refers to the origination of changes to the db/model structure.

Gerald Davis
  • 4,541
  • 2
  • 31
  • 47
  • Explanation was clear and smooth. I actually came to conclusion that using the Generate SQL command is a great way to update model, then I need to push changes manually using old-school techniques – usr-local-ΕΨΗΕΛΩΝ May 11 '15 at 15:50