EF代码首次更新数据库遇到错误“数据库中已经有一个名为“产品"的对象". [英] EF Code First Update Database Encountered Error "There is already an object named 'Products' in the database"

本文介绍了EF代码首次更新数据库遇到错误“数据库中已经有一个名为“产品"的对象".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MVC4中使用EF5代码优先.在某些表中,字段具有十进制数据类型.我只是想将小数点的精度从<18> 2最初更新为(18,2).请帮忙.

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.

为了实现这一目标,我刚刚修改了OnModelCreating,就像这样:

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

        }

那我就跑了:

PM> update-database -verbose 命令

但是我得到了错误:

数据库中已经有一个名为产品"的对象.

迁移产生的代码

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])
)

迁移表

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

推荐答案

我遇到了同样的问题,在尝试添加新迁移并使用它们更新数据库架构后,没有成功,我删除了所有迁移源文件,并且删除了在dbo_MigrationHistory表上找到的迁移条目行.之后,我添加了一个新的迁移,并能够更新架构更改.

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.

我希望有帮助.

这篇关于EF代码首次更新数据库遇到错误“数据库中已经有一个名为“产品"的对象".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆