Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Entity Framework from Database First to Code First

I am trying to convert an existing data model from Database First to Code First.

Background

The current solution (put in place before me) uses a Database project to define the model. This is then published to a database, and we then update an EDMX model from said database.

Finally, a couple of T4 templates are run to generate POCO classes from the EDMX model, as well as the DBContext.

I want to get rid of this, and move purely to a Code First migration approach.

What I've Done So Far

  • I have taken the POCO classes that were generated by the T4 template, and made them first class citizens of the project.
  • I then removed the T4 template from the solution
  • I took the DBContext that was generated by the T4 template, and also made it a first class citizen of the project.
  • I then removed this T4 template from the solution
  • From the Package Manager Console, I ran "Enable-Migrations", and it created my Migrations folder

I also changed the connection string to use the System.Data.SqlClient provider. Was:

<add name="MyContext" connectionString="metadata=res://Project.Data/Model.MyModel.csdl|res://Project.Data/Model.MyModel.ssdl|res://Project.Data/Model.MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MY-SERVER;initial catalog=MY-DB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Changed to:

<add name="MyContext" connectionString="data source=MY-SERVER;initial catalog=MY-DB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

Here is where I am having an issue

I am now trying to create the initial migration, passing the -IgnoreChanges flag so that I get an empty migration (given the age of the database, I want future migrations to be based off the current schema, and not create a migration from scratch).

When I run: Add-Migration InitialCreate -IgnoreChanges

I get this error:

Unable to load the specified metadata resource.

When I run: Add-Migration InitialCreate -IgnoreChanges -ConnectionString "data source=MY-SERVER;initial catalog=MY-DB;Integrated Security=SSPI;" -ConnectionStringProviderName "System.Data.SqlClient"

I get this error:

Can not override the connection for this context with a standard DbConnection because the original connection was an EntityConnection.

At a loss here. It appears that even though I've removed references to the EDMX model, the context still knows about it. I'd like to get rid of it completely and go pure Code First.

Any help appreciated.

like image 775
Brendan Green Avatar asked Dec 06 '25 04:12

Brendan Green


1 Answers

Just comment out the contents of the InitialCreate.cs then run in PM

update-database

new migrations will work correctly after that.

like image 61
Kirsten Avatar answered Dec 09 '25 19:12

Kirsten