rockenpeace
rockenpeace

Reputation: 1019

There is already an object named 'tableName' in the database

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

Upvotes: 2

Views: 3376

Answers (1)

Ash8087
Ash8087

Reputation: 701

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.

Upvotes: 1

Related Questions