1

我在 MVC4 中使用 EF5 Code First。有些表的字段具有十进制数据类型。我只是想将小数的精度从 (18,2) 更新为 (18,4)。请帮忙。

为了实现这个目标,我刚刚修改了 OnModelCreating ,如下所示:

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

迁移表

在此处输入图像描述

堆栈跟踪

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
4

2 回答 2

2

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

我希望这会有所帮助。

于 2015-01-04T03:57:30.793 回答
0

这可能会迟到,但上面的答案并没有解决我的问题,而是手动删除所有表,然后update-database -force在包管理器控制台中使用为我完成了这项工作......

于 2015-11-13T18:58:27.573 回答