Update database with Entity Framework 6

1

Well I'm using Visual Studio 2017, I have a project in C # which has installed Entity Framework in version 6.2.0 in addition to using SQL Server 2008. (I'm not using MVC but Web API)

What I need is to be able to map my database model in the Visual Studio project to my database in SQL Server, although entity framework allows me to map it with the option in the entity viewer called "Generate base of data from model ... "this deletes all database records in SQL Server, to which I need the data already registered not to be deleted.

Is there any way to do this? I tried it with "Code First Migrations" but when I made the Enable-Migrations I got this error:

  

Creating a DbModelBuilder or writing the EDMX from a DbContext created   using Database First or Model First is not supported. EDMX can only be   obtained from a First Code DbContext created without using an existing   DbCompiledModel.

However, if you create the Migrations folder and execute the command

  

add-migration InitialCreate

marks me the previous error "Creating a DbModelBuilder ..."

and no class is generated with the changes that have been made in the model.

It should be noted that the entities generated by the Entity Framework of the Database are in a separate project called Entities.

    
asked by José MN 26.01.2018 в 02:46
source

1 answer

-1

The problem is that using model-first is designed for the case of using a model that will not evolve over time. There is very little documentation for model-first, which at first seems to be ideal for a software architect. After a while, you realize that certain use cases are not supported (migrations).

I suggest you generate another model from the database (database-first). If you correctly map the foreign keys, you will realize that it is almost the same as your model created with model-first.

Code-first is for when you create classes that represent tables and their relationships. If you did not start the project with code first, I suggest you use database-first to not break your current database.

    
answered by 26.01.2018 в 05:45