我在执行 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 背景知识有关。预先感谢您的帮助!
所以我可能会误解你所拥有的内容,但是你不要在定义中创建一次列:
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 ";