EF-Code cannot be used to rebuild the database.

ef-code-first entity-framework

Question

I'm attempting to learn.EF Code first . Initially, I used DataAnnotation on the ModelClass to test every constraint. And they perform well.
But now I'm doing the same thing usingFluent API The database cannot be recreated using standalone configuration files, and this error is returned as a result.

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.

This is my model.

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

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

I have attached 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 am unable to identify my errors. Every input is welcome.

I am use EF 4.3.1.

Edit
The Fluent API is effective if I put the setting in theOnModelCreating technique, such

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

But I'm not sure why it won't run with a standalone configuration file, i.e.UserConfiguration class. Any advice, please

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

Accepted Answer

The error occurred in another area of the program, and as a result, the database schema but not the table were generated.dbo._MigrationHistory is unable to create on the sqlServer, which is why Codefirst cannot delete the database since the necessary information is missing. When I erased the database, it mostly succeeded, but I believe that browsers are still displaying cached data. As soon as I rebooted the machine, everything was OK.

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