我创建了一个种子方法,但是无法填充我的“Ticket”表。另外两张表已经填好了。
以下是程序包管理器控制台中显示的错误:
Cannot insert the value NULL into column 'TicketID', table 'OnlineTicketSystemContext.dbo.Tickets';
column does not allow nulls.
INSERT fails.
这是“Ticket”表的种子方法,它不会填充:
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();
这是Ticket模型:
public class Ticket
{
public int TicketID { get; set; }
public int EmployeeID { get; set; }
public int CustomerID { get; set; }
public string Summary { get; set; }
public DateTime StartDate { get; set; }
public DateTime DueDate { get; set; }
public Boolean HardwareDelivered { get; set; }
public Status? Status { get; set; }
public Priority? Priority { get; set; }
public virtual Employee Employee { get; set; }
public virtual Customer Customer { get; set; }
答案 0 :(得分:0)
我按如下方式进行自动迁移:
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;
将解决该问题。或者您可以删除该表并再次进行迁移。