Entity framework CTP5 Model compatibility cannot be checked because the database does not contain model metadata - entity-framework

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>();
}

Related

Entity Framework Core - Changing Database Schema at runtime without running migration

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?

Why can't I use ApplyConfiguration() to add my audit log table (Audit.NET can't find it)?

I have an existing application that is built on Entity Framework Core 2.2.x. It is using modelBuilder.ApplyConfiguration() to associate entities with the data model dynamically. This works for all of the current entities and even my new AuditLog entity as far as the rest of the application is concerned.
However, when I configure Audit.NET's entity framework core provider to log into AuditLog, the data provider cannot write to the database:
The entity type 'AuditLog' was not found. Ensure that the entity type has been added to the model.
I have scoured the internet for solutions to that error, and found that adding this line to my code will cause Audit.NET to find my AuditLog:
modelBuilder.Entity<AuditLog>().ToTable("AuditLog", "Audit");
My code:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Type[] maps = EntityFrameworkReflectionMapping.Get(EntityTypeConfiguration(), BoundAssemblies);
foreach (object instance in maps.Select(Activator.CreateInstance))
modelBuilder.ApplyConfiguration((dynamic)instance);
modelBuilder.Entity<AuditLog>().ToTable("AuditLog", "Audit");
base.OnModelCreating(modelBuilder);
}
Why do I need to add the entity explicitly, when the rest of the system works as-is?
Additionally, the changes are being detected by Audit.NET through entities which are not explicitly added. So the problem seems to be with Audit.NET's entity framework data provider, or how I'm using it.
I would expect that the data provider would respect the modelBuilder.ApplyConfiguration() approach to associating entities.
There are many things that could be causing the exception, but looks like EF is not being able to detect the entity-table relation for your AuditLog.
Look for a wrong connection string, maybe your AuditLog being defined on a different assembly than other entities.
Also try adding the AuditLog entity class within a db set as a property on your DbContext, for example:
public class MyContext : AuditDbContext
{
//...
public DbSet<ModelName> ModelName { get; set; }
}

Identity 2.0 CodeFirst Migration to Use Stored Procedures

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?

Add schema to each query in Entity Framework 6

Firstly, I am new to EF but am an experienced NHibernate user...
I am trying to update an existing EF repository layer that is talking to an existing database to automatically include a schema in every query submitted to the the database. To clarify:
The database tables already exist, but they have been moved to a new schema in SQL Server
The C# class model already exists
The database isn't created each time as is already exists, so the OnModelCreating method of the DbContext is never called. The classes have [Key] etc. from the DataAnnotations namespace, so I'm guessing it's code first.
In NHibernate, this is easy as I can add a default schema when building the configuration, so that every query automatically has the schema added to it regardless of the existing class model - the database and class model don't need to be updated.
Is this possible in Entity Framework and if so, where would I put this override? If not, how can I change EF to inject a schema to each query? Can I override the query generation function?
Thanks
In your DbContext add the following
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("myNewSchema");
}
if there is no schema defined elsewhere this will take care of it globally. I think that an explicitly defined schema elsewhere will override the default schema you set here.

Entity Framework database-first and automatically setting concurrency token for all tables

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?