2

i want to create database and i use entity framework but when i run my wpf project, i take this error. This error appears for 2 tables. In here, my error:

using (var c = new RSPDbContext()) {
            var s = from v in c.Users select v;
        }

In RSPDbContext.cs, i generate my database.

public RSPDbContext()
        : base("databaseName")
    {
        Database.SetInitializer<RSPDbContext>((IDatabaseInitializer<RSPDbContext>)new MigrateDatabaseToLatestVersion<RSPDbContext, RSP.Common.Migrations.Configuration>());
    }

and Configuration.cs:

public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

In app.config:

<connectionStrings>
    <add name="databaseName" connectionString="Data source=.\SQLExpress;Initial catalog=databaseName;Integrated Security=true; MultipleActiveResultSets=True;" providerName="System.Data.SQLClient" />
</connectionStrings>

In RSDPContext.cs, i can create smoothly when i eject this table:

public DbSet<tableName> tableNames { get; set; }

why i take this error ? any idea ? thanks in advance

rockenpeace
  • 1,011
  • 5
  • 18
  • 29

1 Answers1

1

If you started using Migrations after your db was already made, you might see this error since EF doesn't know that this db does not need migrating.

The simplest way to get past this problem is to drop your existing database and see if your app can create a new, clean database. (Obviously, you can't do this if you have a live, production db.) Otherwise, you need to create an initial migration and force it to apply.

Community
  • 1
  • 1
Ash8087
  • 643
  • 6
  • 16