EF-Code cannot be used to rebuild the database.

ef-code-first entity-framework

Question

I am trying to learn EF Code first. Initially I have tried all constraints using DataAnnotation on the ModelClass. And the work fine.
But now i am trying same using Fluent API With standalone configuration Files but It is unable to recreate the database and throwing this 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.

Here is my Model

public Class User
{
    public string UserID { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

Here is my DbContext

public class AppContext : DbContext
{
    public DbSet<User> Users { get; set; }

    //Public constructor to Call DropCreateDatabaseIfModelChanges method
    public AppContext()
    { 
        //Calling the Method to ReCreate the database
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AppContext>());
    }

    //Overriding OnModelCreating Method to Configure the Model using Fluent API
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Configuration.Add(new UserConfiguration());
    }
}

Here is my UserConfiguration Class

public class UserConfiguration : EntityTypeConfiguration<User>
{
     public UserConfiguration()
     {
          //Using Fluent API to configure Model
          HasKey(x => x.UserID);
          Property(x => x.Username).IsRequired().HasMaximumLength(50);
          Property(x => x.Password).IsRequired().HasMaximumLength(50);

     }
}

I Cannot figure out where I am Wrong. Any suggestion is appreciable.

Note : I am using EF 4.3.1

Edit
The Fluent API is working well if i put the configuration directly in the OnModelCreating method i.e

//Overriding OnModelCreating Method to Configure the Model using Fluent API
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         //Instead of Standalone Configuration file
         //modelBuilder.Configuration.Add(new UserConfiguration());

         //putting configuration here only
          modelBuilder.Entity<User>().HasKey(x => x.UserID);
          modelBuilder.Entity<User>().Property(x => x.Username).IsRequired().HasMaximumLength(50);
          modelBuilder.Entity<User>().Property(x => x.Password).IsRequired().HasMaximumLength(50);
    }

However i dont know the reason why its not working with standalone configuration file i.e UserConfiguration class.Any suggestion please

1
0
5/12/2012 5:18:03 PM

Accepted Answer

The Error was occurring in some other part of application, due to which database schema is created but the table dbo._MigrationHistory is unable to create on the sqlServer i.e is why the Codefirst is not able to drop the database coz the required info is not there. when i deleted the database it mst worked but i guess browers is showing the cached data. Everything is well when i restarted the system.

0
5/12/2012 5:53:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow