【问题标题】:Error when using Update-Database command MVC 4使用更新数据库命令 MVC 4 时出错
【发布时间】:2014-04-21 14:12:58
【问题描述】:

我创建了一个种子方法,但它无法填充我的“票证”表。其他两个表已被很好地填充。

这是包管理器控制台中显示的错误:

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

这是票证模型:

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

【问题讨论】:

  • 数据库中的 TicketID 是否设置为自动递增?还是需要添加?
  • 我认为它应该默认自动递增?我的其他表自动递增,无需任何数据注释
  • 可能是本专栏的数据库设置错误,我去看看。

标签: c# asp.net-mvc-4 entity-framework-migrations package-managers seed


【解决方案1】:

我按如下方式进行自动迁移:

  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; 会解决这个问题。或者您可以删除表并再次进行迁移。

【讨论】:

  • 问题是,我的配置文件中也已经有了这些确切的设置
  • 然后看前面的值。你可能需要手动清理一些东西。
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多