Question

I have created a seed method and it is failing to populate my 'Ticket' table. The other two tables have been populated fine.

Here is error displayed in the Package Manager Console:

Cannot insert the value NULL into column 'TicketID', table 'OnlineTicketSystemContext.dbo.Tickets';
column does not allow nulls.
INSERT fails.

Here is my seed method for the 'Ticket' table which will not populate:

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

Here is the Ticket model:

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; }
Was it helpful?

Solution

I do automatic migrations as follows:

  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; will sort out that problem. or you can remove the table and do migrations again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top