C# 使用更新数据库命令MVC 4时出错
我创建了一个seed方法,但它无法填充我的“Ticket”表。其他两个表填充得很好 以下是Package Manager控制台中显示的错误:C# 使用更新数据库命令MVC 4时出错,c#,asp.net-mvc-4,entity-framework-migrations,package-managers,seed,C#,Asp.net Mvc 4,Entity Framework Migrations,Package Managers,Seed,我创建了一个seed方法,但它无法填充我的“Ticket”表。其他两个表填充得很好 以下是Package Manager控制台中显示的错误: Cannot insert the value NULL into column 'TicketID', table 'OnlineTicketSystemContext.dbo.Tickets'; column does not allow nulls. INSERT fails. 以下是我对“票证”表的种子方法,它不会填充: var tickets
Cannot insert the value NULL into column 'TicketID', table 'OnlineTicketSystemContext.dbo.Tickets';
column does not allow nulls.
INSERT fails.
以下是我对“票证”表的种子方法,它不会填充:
var tickets = new List<Ticket>
{
new Ticket{
EmployeeID = employees.Single(s => s.Surname == "Alexander").EmployeeID,
CustomerID = customers.Single(c => c.Surname == "Marsden").CustomerID,
Summary = "Broken laptop screen",
StartDate = DateTime.Parse("04/05/2012"),
DueDate = DateTime.Parse("10/05/2012"),
HardwareDelivered = true,
Status = Status.Open,
Priority = Priority.High
},
new Ticket{
EmployeeID = employees.Single(s => s.Surname == "Marshall").EmployeeID,
CustomerID = customers.Single(c => c.Surname == "Copper").CustomerID,
Summary = "Keyboard doesnt work",
StartDate = DateTime.Parse("09/07/2012"),
DueDate = DateTime.Parse("12/07/2012"),
HardwareDelivered = true,
Status = Status.Open,
Priority = Priority.High
}
};
foreach (Ticket t in tickets)
{
var ticketInDataBase = context.Tickets.Where(
s =>
s.Employee.EmployeeID == t.EmployeeID &&
s.Customer.CustomerID == t.CustomerID).SingleOrDefault();
if (ticketInDataBase == null)
{
context.Tickets.Add(t);
}
}
context.SaveChanges();
我按如下方式执行自动迁移:
internal sealed class Configuration : DbMigrationsConfiguration<NetCollab.Web.Data.DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "NetCollab.Web.Data.DataContext";
}
protected override void Seed(NetCollab.Web.Data.DataContext context)
{
// This method will be called after migrating to the latest version.
}
}
AutomaticMigrationDataLossAllowed=true;我会解决那个问题的。或者,您可以删除该表并再次执行迁移 问题是,我的配置文件中已经有了这些精确的设置,看看前面的值。您可能需要手动清理某些内容。数据库中的TicketID是否设置为自动递增?或者你需要添加它吗?我想它应该默认自动递增?我的其他表是自动递增的,不需要任何数据注释。可能这列的数据库设置错误,我想看一下。
internal sealed class Configuration : DbMigrationsConfiguration<NetCollab.Web.Data.DataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "NetCollab.Web.Data.DataContext";
}
protected override void Seed(NetCollab.Web.Data.DataContext context)
{
// This method will be called after migrating to the latest version.
}
}