执行update-database时出现错误,指出:“每个表中的列名必须是唯一的。表'dbo.OrderForms'中的列名'OrderRecieveDate'被指定多次。”我已经尝试了所有我能想到的东西来修复它,到目前为止还没有任何事情发生。以前我能够通过我创建的网站从我的表中添加/更新/删除行,但现在该功能似乎无法正常工作。我甚至无法让数据库在此时添加我的种子数据。
我的上下文/模型文件:
public class OrderFormContext : DbContext
{
public DbSet<OrderForm> OrderForms { get; set; }
}
public class OrderForm
{
[Key, Display(Name= "Order ID" )]
[ScaffoldColumn(false)]
public int OrderID { get; set; }
private DateTime _date = DateTime.Today;
[Display(Name = "Date Posted")]
[ScaffoldColumn(false)]
public DateTime OrderPostDate
{
get { return _date; }
set { _date = value; }
}
[EnumDataType(typeof(PersonReqID)),Display(Name = "Person Requesting")]
public PersonReqID PersonReq { get; set; }
[StringLength(100), Display(Name = "Grant")]
public string GrantID { get; set; }
[StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
public string ItemDescription { get; set; }
[StringLength(100), Display(Name = "Quantity")]
public string ItemQuantity { get; set; }
[StringLength(100), Display(Name = "Price")]
public string UnitPrice { get; set; }
[StringLength(100), Display(Name = "Company")]
public string CompanyID { get; set; }
[StringLength(100), Display(Name = "Catalog #")]
public string CatalogNum { get; set; }
[ScaffoldColumn(false)]
[Range(typeof(DateTime), "1/1/1111", "1/1/3000", ErrorMessage = "Please provide a date.")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime OrderMadeDate { get; set; }
[ScaffoldColumn(false)]
[Range(typeof(DateTime), "1/1/1111", "1/1/3000", ErrorMessage = "Please provide a date.")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime OrderRecieveDate { get; set; }
[ScaffoldColumn(false)]
[EnumDataType(typeof(PersonRecID)), Display(Name = "Person Recieving")]
public PersonRecID PersonRec { get; set; }
[ScaffoldColumn(false)]
[StringLength(100), Display(Name = "Item Location")]
public string ItemLoc { get; set; }
}
我的Configuration.cs文件:
context.OrderForms.AddOrUpdate(
new OrderForm
{
OrderID=1,
OrderPostDate=DateTime.Now,
PersonReq=PersonReqID.Jake,
GrantID="NASA",
ItemDescription="Grenades",
ItemQuantity="100",
UnitPrice="1.50",
CompanyID="Grenades UNLMTD",
CatalogNum="G1001",
PersonRec=PersonRecID.Ajit,
ItemLoc="Freezer"
}
);
context.SaveChanges();
最后我的xxxxxxx_initial.cs迁移文件:
public override void Up()
{
AddColumn("dbo.OrderForms", "OrderRecieveDate", c =>
c.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
AddColumn("dbo.OrderForms", "OrderMadeDate", d =>
d.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
}
public override void Down()
{
DropColumn("dbo.OrderForms", "OrderRecieveDate");
DropColumn("dbo.OrderForms", "OrderMadeDate");
}
我对网页设计很陌生,所以我只是总结一下我认为可能导致问题的一切。这是我的OrderForms.dbo文件的表定义。
CREATE TABLE [dbo].[OrderForms] (
[OrderID] INT IDENTITY (1, 1) NOT NULL,
[GrantID] NVARCHAR (100) NULL,
[ItemDescription] NVARCHAR (MAX) NULL,
[ItemQuantity] NVARCHAR (100) NULL,
[UnitPrice] NVARCHAR (100) NULL,
[CompanyID] NVARCHAR (100) NULL,
[CatalogNum] NVARCHAR (100) NULL,
[OrderDate] NVARCHAR (100) NULL,
[ItemLoc] NVARCHAR (100) NULL,
[PersonReq] INT DEFAULT ((0)) NULL,
[PersonRec] INT DEFAULT ((0)) NULL,
[OrderPostDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
[OrderRecieveDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
[OrderMadeDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
CONSTRAINT [PK_dbo.OrderForms] PRIMARY KEY CLUSTERED ([OrderID] ASC)
);
非常感谢任何帮助。我似乎无法弄清楚如何创建列两次。我尝试删除数据库和以前的迁移并执行'enable-migrations -forced'并从那里重新启动无效。这可能与我最终缺乏关于EF的背景知识有关。在此先感谢您的帮助!
答案 0 :(得分:0)
所以我可能会误解你的内容,但是你不要在定义中创建一次列:
CREATE TABLE [dbo].[OrderForms] (
[OrderID] INT IDENTITY (1, 1) NOT NULL,
[GrantID] NVARCHAR (100) NULL,
[ItemDescription] NVARCHAR (MAX) NULL,
[ItemQuantity] NVARCHAR (100) NULL,
[UnitPrice] NVARCHAR (100) NULL,
[CompanyID] NVARCHAR (100) NULL,
[CatalogNum] NVARCHAR (100) NULL,
[OrderDate] NVARCHAR (100) NULL,
[ItemLoc] NVARCHAR (100) NULL,
[PersonReq] INT DEFAULT ((0)) NULL,
[PersonRec] INT DEFAULT ((0)) NULL,
[OrderPostDate] DATETIME DEFAULT ('1900-01-01T00:00:00.000') NULL,
[OrderRecieveDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
[OrderMadeDate] DATETIME DEFAULT ('2013-01-01T00:00:00.000') NULL,
CONSTRAINT [PK_dbo.OrderForms] PRIMARY KEY CLUSTERED ([OrderID] ASC)
然后尝试再次创建它们
public override void Up()
{
AddColumn("dbo.OrderForms", "OrderRecieveDate", c =>
c.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
AddColumn("dbo.OrderForms", "OrderMadeDate", d =>
d.DateTime(nullable: false, defaultValue: new DateTime(1000, 1, 1)));
}
如果您需要一些故障排除建议,可以使用sys.tables和sys.columns查找值,并在添加列上使用IF NOT EXISTS。
这需要一些修改,但它可能足以让你前进。
string columnToAdd = " ADD [" + columnList[i] + "] " + columnDataTypeList[i];
string alterTableSQL = @"ALTER TABLE " + DestinationTbl + columnList[i];
string columnMakerSQL += @"IF NOT EXISTS ( SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'" + DestinationTbl
+ @"') AND name = '" + columnList[i]
+ @"' ) BEGIN "
+ alterTableSQL
+ @" END ";