Entity framework code first - work with existing empty database - entity-framework

Working on EF Codefirst 5.0, on deployment we created the empty database in order to create the username and permission level settings, because in the database server this user is specific to only this database.
so we done above steps and tried to run the application, it throws below exception as it was not created by the EF code first, any solutions for handling this scenario?
help on this really appreciated.
Error:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.

Related

Adding and running EF core migrations for multiple applications using same DB

I have two different applications (two different solutions) using the same database. Each application has its own set of models/tables used in it.
The .NET solution for Application 1 was created a year back and has evolved till today. As part of this, some new models were created, migrations were added and updated in the database (SampleDatabase). This has its own DBContext class.
The .NET solution for Application 2 is a new one created a month back. This application also uses the same database (SampleDatabase). A few new models specific to this application were created, migrations were added and updated in the database. This has its own DBContext class.
Requirement:
Now, there is a requirement where one of the tables created and being used by Application 1 needs to be modified to integrate with Application 2 (i.e., a PK from an Application 2 table has to be placed as a FK in an Application 1 table.).
Challenge
To create a migration for altering the model from Application 1, a reference is needed to the PK table available in the other solution, which is not possible.
To create a migration for altering the existing model from Application 2, the existing model has to be copied to the Application 2 codebase. By doing so, if a migration is added, it generated only a CreateTable statement and not an alter as the DBContext does not know that the table already exists.
Question:
How do we manage to have migrations that is aware of things happening in the database from both the solutions?
Thoughts:
One way that I had in mind is to have a shared library project that can be responsible for all migrations to this database. Requirements for any application can be taken care by this project. But this has to refer the DBContext from both the application which is again a challenge.
Please let me know your thoughts and if anyone has faced such situation in your projects.
Thank You.

Synchronize single database between EF code first client and and EF database first client apps

Some time ago we started an ETL project which was relatively simple. It was reading source data, transforming it and loading it into a structured, relational database. In the very beginning of the project (when there was no target DB yet) we decided to use EF Core in order to manage schema changes (migrations). It was kind of a good candidate back then and we decided to continue with it.
But, recently we decided to develop a web application. An application which is supposed to read from that existing DB. As we have a lot of entities inside of it (tables), relationships between them and constraints, we decided to reach out to EF Core once again, so it can help us with the generation of the server-side code. As the database was already there, we had no other choice but to try the database first approach.
As described in most of the articles on the internet, we made a reference to the following nuget packages:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
So, with those you have an easy access to Sql servers and you can run commands such as:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
to get your entities and relations between them in your source code. So, the logical question was: How do I make sure that I can always have the latest schema representation in my ORM code?. Obviously, there's no such thing as Update-Database if we were using code first approach. Another option was to force the scaffolding to overwrite everything that I currently have in the repo:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force
but this has some issues such as if you deleted a table (entity) it will still stay in your source code and you will have to manually delete it. So, my question here is what would be the best approach for my use-case.

Core 2 Keep domain models in sync with database changes

Need help! I am new to Core 2.0 and VS 2017 and haven't had much experience with MVC. I have an existing database that I need to use with a core 2.0 project at work. I was able to use the Scaffold-DbContect to initially create the domain models from the existing database.
However, the database developers are making changes to the database and adding new tables. I need to keep my domain models in sync with the database changes that are being made.
The only thing I can find on the internet is how to make changes to the model and update the database schema. However, I need to update the model from changes made to the database.
EF Core works on Code First approach. And You guys are following DB First approach together. So you should make changes in your code and then generate migrations accordingly, Otherwise, it will lead you in trouble.
You can use EF Core Power tool for generating the db changes at code side. But in this case you have to take care while generating migrations from code side.

Manual synchronization between Context and Database using EF Codefirst

I'm using EF 5.0 (CodeFirst) with VS 2012. I changed my model (entity) and manually changed my database. I try to run the application and the following error appears:
The model backing the 'XXXContext' context has changed since the
database was created. Consider using Code First Migrations to update
the database (http://go.microsoft.com/fwlink/?LinkId=238269).
My only change was the name of the entity property (column in the database).
Its automate or ignore the synchronization? For the same has been done manually, only the application that does not recognize it. Or run something that validates manual synchronization.
Where is the recorded synchronization information for the application to know that there was a change?
Thanks
You have the option of using Database Initializers or Migrations. In your application startup you can enable initializers with the following:
System.Data.Entity.Database.SetInitializer<YourDbContextType>(new DropCreateDatabaseIfModelChanges());
You can also subclass and create your own logic if needed. See http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm for more info.
You can also enable migrations and let them automatically update your database. Running Enable-Migrations in the Package Manager Console does this. Look here for more information http://msdn.microsoft.com/en-us/data/jj591621.aspx

Incremental Database development in Entity framework code first

How can I do incremental developments with entity framework code first database. Because if I change something i model classes it will regenerate the database which cased to loss my data already in the database. I'm using DropCreateDatabaseIfModelChanges . Is there any thing other than that to execute alter quires rather than recreating.
EF Code First Migrations would help you here, it's in alpha/CTP currently: Entity Framework Code First Migrations: Alpha, also check out the ADO.NET team blog:
The most consistent request we have heard from you since releasing EF
4.1 has been for a migrations solution for Code First that will
incrementally evolve the database schema as you model changes over
time. Today we are announcing the release of our first Community
Technical Preview (CTP) of our Code First Migrations work.
As I recall, the Microsoft docs tell you to be sure not to use DropCreateDatabaseIfModelChanges in production environments. The point of that option is to help you come up with code-based data population for your test runs. I haven't seen any tools to help with incremental changes when using code-first. Where I work, we use a database-first setup, and we create a change script for each new release that includes alter and insert statements.
incremental database development is not currently available in the current version of the codefirst framework however it is included in the roadmap for the next release which will ship with MVC 4
as of right now you would need to remove metadata tracking from the database conventions and update the database manually via scripting or using the sql tooling until this new convention is added to the framework