Search code examples
c#sql-serverentity-framework-5localdb

Mapping and sql database creation


I started a project and use Entity Framework 5. Now I created a database on my SQL Server Express with all tables. Further I created the DbContext with a fluent mapping.

What is better, the fluent mappings or the .edmx mapping files?

The database is now on the SQL Server but I want support also SQL local db.

Is there a way to tell the EF that I want to use a SQL local db?

Should I ship the whole database within the setup or better to create the database on startup of my application? How can I use EF to create the database (SQL server or SQL local db)?

Every useful help would be appreciated.


Solution

  • If you are using EF5 I would stay away from edmx. You could reverse engineer your model from database using Entity Framework Power Tools. Than you can customize your model using either Data Annotations or fluent mappings.

    If you use EF5 code first it can create database automatically for you if not present, however that would not work very well on subsequent upgrades (it can recreate database but then you will use or your existing data). The options you could use, is either EF migrations, where you can specify in fluent-like languagage the modifications that were made to your database or use Database Project in Visual Studio, where you can store all your schema in source control and then generate database upgrade scripts betweeen releases.