问题
I'm using EF5 Code First in MVC4. There are tables where fields have decimal datatypes. I just wanted to update the precision of decimal originally from (18,2) to (18,4). Please help.
To achieve the goal I just modified the OnModelCreating something like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<DealerLevelDiscount>().Property(d =>d.Discount ).HasPrecision(18, 4);
modelBuilder.Entity<DealerTransaction>().Property(d => d.ProductDiscount).HasPrecision(18, 4);
modelBuilder.Entity<DealerTransaction>().Property(d => d.VolumeDiscount).HasPrecision(18, 4);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
Then I just ran:
PM> update-database -verbose command using Package Manager Console
But I got error:
There is already an object named 'Products' in the database.
Code Generated by Migration
PM> update-database -verbose
Using StartUp project 'SharkStore.WebUI'.
Using NuGet project 'SharkStore.Infrastructure'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'SharkStore' (DataSource: (local), Provider: System.Data.SqlClient, Origin: Configuration).
Applying code-based migrations: [201304270821111_InitialCreate, 201305041132116_updateDatabae, 201305111219416_5112013, 201305141351533_5142013, 201305231451514_5232013, 201306130611074_06132013_updates, 201306221339425_06222013, 201306231051456_06232013, 201306251538120_06251013, 201306251611006_06262013_01, 201307021414262_07022013, 201307040902338_742013, 201307140659150_07142013, 201307140701597_07142013_1, 201308050843017_852013, 201309270307238_09272013].
Applying code-based migration: 201304270821111_InitialCreate.
CREATE TABLE [dbo].[Products] (
[Id] [int] NOT NULL IDENTITY,
[Thumbnail] [nvarchar](max),
[MainImage] [nvarchar](max),
[FileData] [nvarchar](max),
[Name] [nvarchar](max),
[ShortDescription] [nvarchar](max),
[LongDescription] [nvarchar](max),
[Price] [decimal](18, 2) NOT NULL,
[DownloadCount] [int] NOT NULL,
[BHMinVersion] [nvarchar](max),
[BHMinEdition] [nvarchar](max),
[TrialPeriod] [int] NOT NULL,
[ProductTypeId] [int] NOT NULL,
[CreatedBy] [int] NOT NULL,
[IsDeleted] [bit] NOT NULL,
[ModifiedBy] [int] NOT NULL,
[CreatedDate] [datetime],
[ModifiedDate] [datetime],
CONSTRAINT [PK_dbo.Products] PRIMARY KEY ([Id])
)
Migration Table
Stacktrace
System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Products' in the database.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:bcd5bb53-2e1d-4121-8aae-4ebfe353ebf6
回答1:
I was having the same issue and after trying to add new migrations and update the database schema with them, without success, I deleted all the migration source files and I deleted the migration entries rows found on the dbo_MigrationHistory table. After that I added a new migration and was able to update schema changes.
I hope this help.
回答2:
This might be late, but the above answer did not solve my problem but manually dropping all the tables and then using update-database -force
in the package manager console did the job for me......
来源:https://stackoverflow.com/questions/19542462/ef-code-first-update-database-encountered-error-there-is-already-an-object-name