簡體   English   中英

使用更新數據庫命令MVC 4時出錯

[英]Error when using Update-Database command MVC 4

我創建了一個種子方法,但無法填充“票證”表。 其他兩個表已填充好。

這是程序包管理器控制台中顯示的錯誤:

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();

這是票證模型:

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; }

我執行自動遷移,如下所示:

  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; 將解決該問題。 或者您可以刪除表並再次進行遷移。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM