我在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&GT;使用程序包管理器控制台
的update-database -verbose 命令但我收到了错误:
数据库中已有一个名为“Products”的对象。
迁移生成的代码
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
答案 0 :(得分:2)
我遇到了同样的问题,在尝试添加新迁移并使用它们更新数据库架构后,我没有成功删除了所有迁移源文件,并删除了dbo_MigrationHistory表中的迁移条目行。之后,我添加了一个新的迁移,并能够更新架构更改。
我希望这有帮助。
答案 1 :(得分:0)
这可能会迟到,但上面的答案并没有解决我的问题,但手动删除所有表,然后在包管理器控制台中使用update-database -force
为我做了工作......