EF - Create missing SQL Table/Object from Entity Object - sql

Is there any function in the .NET Entity Framework, that can create missing SQL table. Or stored procedure or any other SQL Object?
Have do some research and did not found anything yet.

You could use code first migrations (either automatic or code based). If you are adding table to existing database create initial migration first and than add your new entity. All links are for EF 4.3 but the usage is exactly the same in EF 5.
Taken from here

Related

Entity Framework update database

Using Entity Framework 6 (with code first approach), there is a exception if the database is not up to date
System.InvalidOperationException: "The model backing the 'xxx' context has changed since the database was created.
Is there a way to check, if the database is up to date and if not, update the database to the latest version from within the application itself?
Keeping your Database up to date with the EF Model is paramount.
For every change you make to the model of your context, there must be a migration to the database.
To maintain the integrity of the relationship between model and db you must:
Add-Migration migrationName_versionx.x.x
Update-Database
The exception you have experienced System.InvalidOperationException: is telling you that you that your model has not been migrated into your database. This means that EF is not able to work properly with possible queries you mave have.
Your database will keep a migration history which you can use to validate the integrity. If you are in doubt, simply apply a new migration and see if there were any changes.
This works using the DBMigrator Class
Dim migrator = New DbMigrator(new migrations.Configuration())
migrator.Update()

Scaffold-DbContext for database views in EF Core 2.1 (Query Types)

EF Core 2.1 has new feature - Query Types.
Some of the main usage scenarios for query types are:
Serving as the return type for ad hoc FromSql() queries.
Mapping to database views.
Mapping to tables that do not have a primary key defined.
Mapping to queries defined in the model.
I upgrade project to Core 2.1, but Scaffold-DbContext still does not generate database views. I have to use a special parameter or the Scaffold-DbContext does not support it?
Here's a hackish but working solution:
How to Scaffold Controllers with database views to EF Core 2.1
Create view in database.
Create a POCO with same structure as view.
Add a new Controller with POCO created in step#2
a. If key related error occurs, add a Key attribute on a column and then remove after scaffolding is completed.
A new property with DbSet<T> should have gotten added where T is the class created in step#2. Change DbSet to DbQuery.
In OnModelCreating method of DbContext, add following code:
modelBuilder.Query<POCO from step#2>().ToView("Name of the view");
Source
Not supported in 2.1. See issue #1679.

How to update database `jsonb` column during migration in EF Core?

I develop ASP.NET Core application and use EF code first approach. Application have already deployed to the production and we have user data. Now we updated the model of column which has type jsonb. So, we need to refresh this column in existing records acording to the new model. What is the best approach for such update? Is it possible somehow to make this with EF migration mechanism?

Is it possible to create an ASP .NET Core Migration with data in it?

I am working with migrations in ASP .NET Core and Entity Framework and everything works well to preserve the structure of tables.
However, I would like (if possible) to also have data in migrations, so that a person who checkout my code (and migrations) can also retrieve that data (which would typically be test data or data that is stored in the database).
Is is possible? If not, what would be an alternative?
For example FluentMigrator allows that type of migration :
Insert.IntoTable("TestTable").Row(new { Name = "Test" });
In Entity Framework, migrations are used for creating Database Schema (not the data itself). If you need some type of data that should be included in the database while creating it, you should Seed the data while creating the schema.
If you want that one person to also retrieve your data then the connection string of that particular application should point to the same shared database where data actually resides.

Sql Azure CompatibleWithModel not working

In my code I am trying to check if my entity framework Code First model and Sql Azure database are in sync by using the "mycontext.Database.CompatibleWithModel(true)". However when there is an incompatibility this line falls over with the following exception.
"The model backing the 'MyContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data."
This seems to defeat the purpose of the check as the very check itself is falling over as a result of the incompatibility.
For various reasons I don't want to use the Database.SetInitializer approach.
Any suggestions?
Is this a particular Sql Azure problem?
Thanks
Martin
Please check out the ScottGu blog below:
http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
Here is what is going on and what to do about it:
When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data. The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database). Existing databases won’t have the EdmMetadata table and so won’t have the hash…and the implementation today will throw if that table is missing. We'll work on changing this behavior before we ship the fial version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:
Database.SetInitializer<Production>(null);
Using above code you are no recreating the database instead using the existing one so I don't think using Database.SetInitializer is a concern unless you have some serious thoughts about using it.
More info: Entity Framework Code Only error: the model backing the context has changed since the database was created