I'm trying to move my existing ASP.Net Identity Framework project across to use Stored Procedures on the existing models. I have migrations already enabled, and have an existing database which is basically the stock/standard the Identity builds (few extra columns here and there). I have tried to enable the Stored Procedures via the DbContext:
public class IdentityDbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("Users").MapToStoredProcedures();
modelBuilder.Entity<ApplicationRole>().ToTable("Roles").MapToStoredProcedures();
}
}
Then I generate the migration:
Add-Migration -Name AddStoredProcedures
And apply the migration:
Update-Database
Now when I try to run my application I am getting the exception when it attempts :
System.InvalidOperationException: The model backing the 'IdentityDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)."}
AFAICT, I ran the update correctly (__migrationhistory table contains the name of the migration). Looking at my database, the stored procedures are added. Why does the framework not recognize it?
Related
I have a class library project in .NET 6 where I am using EF Core. I am able to create tables using the code-first approach, but stuck on how to create stored procedures. I have seen a few articles on Google but it's showing using old EF 6 but not for EF Core.
There is no automatic way to create a stored procedure with EF Core, but the process to do it manually is quite simple.
First create a new migration as you would normally, it should be empty. Then edit the migration to manually create the procedure. For example:
public partial class MyNewStoredProcMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("CREATE PROC DoStuff....");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("DROP PROC DoStuff");
}
}
Note that you may want to move the string for the procedure into a resource file to keep things tidy.
I have a .NET Core 5 application with Entity Framework (code first) with migrations and Azure SQL database.
I want to run two separate applications on same DB with different data. I want to have test and demo application. My idea was to change schema for each application. One application will have schema "test." and another one "demo.".
I tried to do this with this article
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("demo");
}
Problem is that I need to run Add-Migration with this approach. And I don't want to do that.
There is also another way that I didn't try. Described here
Problem there is that I need to create EntityConfiguration for each Entity and I don't want to do this neither.
I want to set schema globally for all tables at once.
Is there a way how to do this without creating new migrations? Or is there a better way without changing scheme?
I am using the entity framework (EF 4.5 using the POCO template that uses the DbContext) and I have a very large model.
Every table in the model has an integer column (named RowVersion). I want to set this column as the concurrency column automatically.
I do not want to manually do this for every table in the EDMX file. And going forward for maintenance I do not want to have to remember to do this.
I was hoping there was something I could add to the context to do this automatically for all tables for me.
I seen this piece of code on another post
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<myEntity>().Property(x => x.RowVersion).IsConcurrencyToken();
}
But this appears to only be for code-first, as the event never gets fired for my model.
Any ideas how I could do this?
I'm trying to update the database on a build server, and it's failing because it's trying to run automatic migrations, even though they're disabled. The database already exists and I just need to apply the latest migration. Here's my context:
public sealed class Configuration : DbMigrationsConfiguration<CableSenseInstanceConfiguratorContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
}
I've got a bunch of migration files I've created manually, here's the latest one:
public partial class Settings : DbMigration
{
public override void Up()
{
AddColumn("dbo.MasterInstances", "Settings", c => c.String());
}
public override void Down()
{
DropColumn("dbo.MasterInstances", "Settings");
}
}
If I then manually update the database from the package manager console, I see it tries to run an automatic migration (which fails because the table already exists):
Applying code-based migrations: [201204200805145_NoMoreCerts, 201210311451543_SuperUsers, 201301041036414_Settings, 201301041128583_Settings2].
Applying code-based migration: 201204200805145_NoMoreCerts.
Applying automatic migration: 201204200805145_NoMoreCerts_AutomaticMigration.
My __MigrationHistory table just has the one entry for the initial creation. How can I stop it from doing the automatic migrations?
Check out the answer from jjslagace here:
Update-Database tries to do an automatic migration even with automatic migrations disabled
You are building your migrations manually. My guess is entity framework wants to add something that you don't have in your migration script, or it wants to name columns differently etc. EF has a brain, and that brain is fairly simple. It's expecting things to be a certain way unless you tell it otherwise using fluent (not by manually creating/tweaking migration files). From the answer on the question above it sounds like sometimes that results in the issue you are seeing.
Long story short don't build the migration files manually. Instead run the add-migrations command. This will create the migration for you and you can see what EF is expecting to do before it's applied to your database (because sometimes it's stupid). If you need to override what EF is generating for you add a fluent mapping in your DBContext class by overriding OnModelCreating. Then just run add-migration again with the -force option. Here is a good reference for using the Fluent API to custimize EF mappings. Rinse and repeat until you get the migration you are looking for then run update-database.
Hope that helps!
I am trying to test the Entity Framework CTP 5 Code First with an existing table.
I defined the model class and DbContext and ran the application. It created the database and table.
I dropped EdmMetadata table from the database.
Added Trusted_Connection=true;Persist Security Info=True in my connection string.
When I run the application again, it gives me this error.
System.NotSupportedException was unhandled by user code
Message=Model compatibility cannot be checked because the database does not
contain model metadata.
Source=EntityFramework
How can I make this application run without EdmMetadata table?
If you don't want to use EdmMetadata table try to add this into your DbContext derived class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}