C# 实体框架“;已经有一个名为';CartProduct&x27;在数据库中。”;
我第一次在PM控制台C# 实体框架“;已经有一个名为';CartProduct&x27;在数据库中。”;,c#,entity-framework,C#,Entity Framework,我第一次在PM控制台add migration shopdmigration中编写并收到了文件 namespace Shop.Migrations { using System; using System.Data.Entity.Migrations; public partial class ShopDBMigration : DbMigration { public override void Up() {
add migration shopdmigration
中编写并收到了文件
namespace Shop.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class ShopDBMigration : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.CartProduct",
c => new
{
CartID = c.Int(nullable: false, identity: true),
ProductID = c.Int(nullable: false),
UserID = c.Int(),
Cookie = c.String(maxLength: 50),
Count = c.Int(nullable: false),
Date = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.CartID);
CreateTable(
"dbo.Category",
c => new
{
ID = c.Int(nullable: false, identity: true),
CategoryID = c.Int(nullable: false),
Link = c.String(nullable: false, maxLength: 50),
Name = c.String(nullable: false, maxLength: 50),
Description = c.String(nullable: false, maxLength: 500),
Keywords = c.String(nullable: false, maxLength: 200),
ParentID = c.Int(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Category", t => t.CategoryID)
.Index(t => t.CategoryID);
CreateTable(
"dbo.Product",
c => new
{
ProductID = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 200),
Description = c.String(nullable: false, maxLength: 400),
Description_Title = c.String(nullable: false, maxLength: 200),
Product_Description = c.String(nullable: false, maxLength: 2000),
Keywords = c.String(nullable: false, maxLength: 200),
Title = c.String(nullable: false, maxLength: 200),
Link = c.String(nullable: false, maxLength: 150),
Count = c.Int(nullable: false),
Price = c.Int(nullable: false),
BarCode = c.String(nullable: false, maxLength: 50),
Image = c.String(nullable: false, maxLength: 200),
Date = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.ProductID);
CreateTable(
"dbo.OrderCustomerInfo",
c => new
{
ID = c.Int(nullable: false, identity: true),
UserID = c.Int(nullable: false),
AllName = c.String(nullable: false, maxLength: 100),
Phone = c.String(nullable: false, maxLength: 12),
Email = c.String(nullable: false, maxLength: 150),
City = c.String(nullable: false, maxLength: 100),
Region = c.String(nullable: false, maxLength: 100),
MailIndex = c.Int(nullable: false),
MailType = c.String(nullable: false, maxLength: 50),
Processed = c.Int(nullable: false),
Additional = c.String(maxLength: 300),
Date = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.User", t => t.UserID, cascadeDelete: true)
.Index(t => t.UserID);
CreateTable(
"dbo.OrderProduct",
c => new
{
ID = c.Int(nullable: false, identity: true),
ProductID = c.Int(nullable: false),
ProductLink = c.String(nullable: false, maxLength: 150),
ProductName = c.String(nullable: false, maxLength: 200),
Count = c.Int(nullable: false),
TotalPrice = c.Int(nullable: false),
Date = c.DateTime(nullable: false),
OrderCustomer_ID = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.OrderCustomerInfo", t => t.OrderCustomer_ID)
.Index(t => t.OrderCustomer_ID);
CreateTable(
"dbo.ProductPhoto",
c => new
{
ProductID = c.Int(nullable: false),
Image = c.String(nullable: false, maxLength: 200),
Alt = c.String(nullable: false, maxLength: 50),
})
.PrimaryKey(t => new { t.ProductID, t.Image, t.Alt });
CreateTable(
"dbo.User",
c => new
{
UserID = c.Int(nullable: false, identity: true),
UserName = c.String(nullable: false, maxLength: 100),
FirstName = c.String(nullable: false, maxLength: 100),
SecondName = c.String(nullable: false, maxLength: 100),
Email = c.String(nullable: false, maxLength: 100),
Password = c.String(nullable: false, maxLength: 100),
Region = c.String(nullable: false, maxLength: 100),
City = c.String(nullable: false, maxLength: 100),
EmailToken = c.String(nullable: false, maxLength: 20),
AuthenticationToken = c.String(nullable: false, maxLength: 20),
Activated = c.Boolean(nullable: false),
Date = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.UserID);
CreateTable(
"dbo.ProductCategories",
c => new
{
Product_ProductID = c.Int(nullable: false),
Category_ID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Product_ProductID, t.Category_ID })
.ForeignKey("dbo.Product", t => t.Product_ProductID, cascadeDelete: true)
.ForeignKey("dbo.Category", t => t.Category_ID, cascadeDelete: true)
.Index(t => t.Product_ProductID)
.Index(t => t.Category_ID);
}
public override void Down()
{
DropForeignKey("dbo.OrderCustomerInfo", "UserID", "dbo.User");
DropForeignKey("dbo.OrderProduct", "OrderCustomer_ID", "dbo.OrderCustomerInfo");
DropForeignKey("dbo.ProductCategories", "Category_ID", "dbo.Category");
DropForeignKey("dbo.ProductCategories", "Product_ProductID", "dbo.Product");
DropForeignKey("dbo.Category", "CategoryID", "dbo.Category");
DropIndex("dbo.ProductCategories", new[] { "Category_ID" });
DropIndex("dbo.ProductCategories", new[] { "Product_ProductID" });
DropIndex("dbo.OrderProduct", new[] { "OrderCustomer_ID" });
DropIndex("dbo.OrderCustomerInfo", new[] { "UserID" });
DropIndex("dbo.Category", new[] { "CategoryID" });
DropTable("dbo.ProductCategories");
DropTable("dbo.User");
DropTable("dbo.ProductPhoto");
DropTable("dbo.OrderProduct");
DropTable("dbo.OrderCustomerInfo");
DropTable("dbo.Product");
DropTable("dbo.Category");
DropTable("dbo.CartProduct");
}
}
}
然后我写了updatedatabase-verbose
,得到了这个错误:
Using StartUp project 'Shop'.
Using NuGet project 'Shop'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'DB_A12B9A_ShopDB' (DataSource: .\LOCALSQL, Provider: System.Data.SqlClient, Origin: Configuration).
Applying explicit migrations: [201701312018367_ShopDBMigration].
Applying explicit migration: 201701312018367_ShopDBMigration.
CREATE TABLE [dbo].[CartProduct] (
[CartID] [int] NOT NULL IDENTITY,
[ProductID] [int] NOT NULL,
[UserID] [int],
[Cookie] [nvarchar](50),
[Count] [int] NOT NULL,
[Date] [datetime] NOT NULL,
CONSTRAINT [PK_dbo.CartProduct] PRIMARY KEY ([CartID])
)
System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'CartProduct' 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, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
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, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, 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.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
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.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
ClientConnectionId:d3c646ad-8f1f-49b1-aef8-b41d89fc1b3c
Error Number:2714,State:6,Class:16
There is already an object named 'CartProduct' in the database.
使用启动项目“Shop”。
使用NuGet项目“Shop”。
指定'-Verbose'标志以查看应用于目标数据库的SQL语句。
目标数据库是:“DB_A12B9A_ShopDB”(数据源:。\LOCALSQL,提供程序:System.Data.SqlClient,源:配置)。
应用显式迁移:[201701312018367_shopdmbmigration]。
应用显式迁移:201701312018367_shopdmbmigration。
创建表[dbo]。[CartProduct](
[CartID][int]不是空标识,
[ProductID][int]不为空,
[UserID][int],
[Cookie][nvarchar](50),
[Count][int]不为空,
[日期][日期时间]不为空,
约束[PK_dbo.CartProduct]主键([CartID])
)
System.Data.SqlClient.SqlException(0x80131904):数据库中已存在名为“CartProduct”的对象。
位于System.Data.SqlClient.SqlConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)
位于System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)
位于System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject StateObjectStateObj、布尔调用方连接锁、布尔异步关闭)
位于System.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior、SqlCommand cmdHandler、SqlDataReader dataStream、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、Boolean和dataReady)
位于System.Data.SqlClient.SqlCommand.RunExecuteOnQueryTds(字符串方法名、布尔异步、Int32超时、布尔异步写入)
位于System.Data.SqlClient.SqlCommand.InternalExecuteOnQuery(TaskCompletionSource`1完成,字符串方法名,布尔sendToPipe,Int32超时,布尔和usedCache,布尔异步写入,布尔inRetry)
位于System.Data.SqlClient.SqlCommand.ExecuteOnQuery()处
在System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.b_u0(DbCommand t,DbCommandInterceptionContext`1c)中
在System.Data.Entity.Infrastructure.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget目标,Func`3操作,TInterceptionContext拦截Context,操作`3执行,操作`3执行)
位于System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand命令,DbCommandInterceptionContext interceptionContext)
位于System.Data.Entity.Internal.InterceptableDbCommand.ExecuteOnQuery()处
位于System.Data.Entity.Migrations.Dbmigator.ExecuteSql(MigrationStatement MigrationStatement,DbConnection connection,DbTransaction transaction,DbInterceptionContext interceptionContext)
位于System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement MigrationStatement,DbConnection connection,DbTransaction transaction,DbInterceptionContext interceptionContext)
位于System.Data.Entity.Migrations.Dbmigator.ExecuteStatementInternal(IEnumerable`1 migrationStatements,DbConnection connection,DbTransaction transaction,DbInterceptionContext interceptionContext)
位于System.Data.Entity.Migrations.DbMigrator.ExecuteStatementSwithintTransaction(IEnumerable`1 migrationStatements,DbTransaction transaction,DbInterceptOnContext InterceptOnContext)
位于System.Data.Entity.Migrations.DbMigrator.ExecuteStatementSwitthingTransaction(IEnumerable`1 migrationStatements,DbConnection connection,DbInterceptOnContext InterceptOnContext)
位于System.Data.Entity.Migrations.Dbmigator.ExecuteStatementInternal(IEnumerable`1 migrationStatements、DbConnection connection、DbInterceptionContext interceptionContext)
位于System.Data.Entity.Migrations.DbMigrator.ExecuteStatementInternal(IEnumerable`1 migrationStatements,DbConnection)
在System.Data.Entity.Migrations.DbMigrator.c_uudisplayClass30.b_uu2e()中
在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.c__DisplayClass1.b__0()中
在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1操作)中
位于System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(操作操作)
位于System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements,DbTransaction existingTransaction)
位于System.Data.Entity.Migrations.Dmbmigator.ExecuteStatements(IEnumerable`1 migrationStatements)
位于System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
位于System.Data.Entity.Migrations.Dbmigator.ExecuteOperations(字符串migrationId,VersionedModel targetModel,IEnumerable`1操作,IEnumerable`1系统操作,布尔降级,布尔自动)
位于System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration,DbMigration lastMigration)
位于System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration,DbMigration lastMigration)
位于System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId)
位于System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations,字符串targetMigrationId,字符串lastMigrationId)
位于System.Data.Entity.Migrations.Dbmigator.UpdateInternal(字符串targetMigration)
在System.Data.Entity.Migrations.DbMigrator.c__DisplayClassc.b__b()中
位于System.Data.Entity.Migrations.Dbmigator.EnsureDatabaseExists(操作必须成功才能访问数据库)
位于System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(操作必须成功才能使用数据库)
位于System.Data.Entity.Migrations.DbMigrator.Update(字符串targetMigration)
位于System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串targetMigration)