Reputation: 6059
I am using EF 4.1 Code First with SQL Server 2008 R2.
I have written my DbContext as following:
public class DataAccessContext : DbContext
{
public DbSet<TipoJob> TipoJobs { get; set; }
public DbSet<Usuario> Usuarios { get; set; }
public DbSet<Telefone> Telefones { get; set; }
public DbSet<Job> Jobs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
And in the global.asax I initialized it:
Database.DefaultConnectionFactory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["DataAccessContext"].ConnectionString);
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataAccessContext>());
But my tables aren't getting created and I receive the following error:
Model compatibility cannot be checked because the EdmMetadata type was not included in the model. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
I already have the database created and a user that is the owner of that database, but the user does not have permission to drop or create a database.
Does anyone know what might be the problem?
Upvotes: 0
Views: 2782
Reputation: 364409
I already have the database created and a user that is the owner of that database, but the user does not have permission to drop or create a database.
That is the problem. All built-in initializers supports only scenario where database doesn't exist. If database exists your initializer thinks it also contains tables and looks for EdmMetadata
table to compare current model hash with a stored one.
You either need to delete database and let your application create it or you must built custom initializer using allowing only table creation in existing database (if you need any DB update you will have to implement that logic as well) - here is some example.
Upvotes: 2
Reputation: 5024
The initializer will try to drop and recreate the database, however like you said, the user isn't allowed to. So yes, you'll get errors.
One of the tables EF CodeFirst tries to create is the EdmMetadata table - it basically just stores a hash of the current model to help it check if the model has changed or not. Since your database user can't drop the database, EF isn't initializing this table - hence the error.
If you're using an existing database, you should remove the metadata table the same way you remove the plural naming:
builder.Conventions.Remove<IncludeMetadataConvention>();
Once you do this though, any changes you make to the model you'll have to make manually in the database. You'll have to come up with a strategy about how you'll migrate database changes from your development machine into production.
Upvotes: 0
Reputation: 6553
Change DropCreateDatabaseIfModelChanges
to DropCreateDatabaseAlways
and run it once, then change it back
Sounds like the model hasn't been changed but the tables or something was manually edited or the EdmMetaData table was deleted
Oh! And make sure this isn't on live! This will drop and re-create all of the tables, so make sure you have a good way to fill them with data.
Upvotes: 0