At work we are developing our latest module in C# Entity framework code-first approach. I have been given the role of developing databases from the DBContexts from within the project/s.

I generated the tables/entities from the DBContext and created database projects and all is fine.

My question is: When a developer changes the DBContext entities or changes a property of an entity, how can I become aware of such changes and update the database projects?


Here is and article explaining a good way to do this will little extra effort. The article describes comparing the SQL Database Project with the EF generated LocalDB to persist schema changes.

Use EF to make schema/entity updates like you would normally in you development workflow. The use the EF database as your comparison DB to update your SQL Database project.

Going the other way means updating objects like stored procedures, logins, additional indexes, and other more administrative/fine tuning objects.

There is a caveat though... EF does not have the tooling to detect these changes automatically, so be disciplined in using the right tools for the right jobs. EF for models and SQL DB Project for... well, everything not controlled with EF code migrations :-)

SQL Database Project and EF

| improve this answer | |

Any serious development process should include some Source Control software and a plan for using the tool. Since Git, Mercurial, SVN, etc are freely available choose one. Git is commonly used by some of the largest organizations so you could choose to use that. There are, of course, many other source control tools.

If you use this approach, regularly checking in the new versions of objects (schemas and code), you will have a trail of previous versions to compare against. Then you can look for changes that affect your code.

Of course, Microsoft's SQL Server Data Tools (SSDT) includes Schema Compare, as do many tools such as Red Gate, Idera, and so forth.

This should give you the tools you need to track the changes in your schemas and changes in the code as well.

| improve this answer | |

You need to look into Migrations. Code First "Migrations" handle incremental changes in the data model. Please see the following two resources to start with:

I assume that the EF source code is already in source control. If you do any manual updates outside of the EF model (indexes perhaps) then you will need to add those to a separate folder in the project and source control so that you have a record of it, and hopefully the check-ins of any "manual" scripts are done at the same time as the code so that the changes can be matched up when viewed historically.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.