Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码没有错误,数据库已连接,但更新条目时出错。有关详细信息,请参见内部异常_C#_Asp.net Mvc_Relational Database_Entity Framework Migrations - Fatal编程技术网

C# 代码没有错误,数据库已连接,但更新条目时出错。有关详细信息,请参见内部异常

C# 代码没有错误,数据库已连接,但更新条目时出错。有关详细信息,请参见内部异常,c#,asp.net-mvc,relational-database,entity-framework-migrations,C#,Asp.net Mvc,Relational Database,Entity Framework Migrations,我正在编写一个MVC web应用程序,并尝试使用代码优先迁移方法填充我的数据库 我在包管理器控制台上执行了以下命令 启用迁移-contexttypename IssueContext 添加迁移初始值创建 更新数据库 然后,我发现更新条目时出错。有关详细信息,请参见内部异常 我的代码没有错误,我的数据库已连接,但数据库资源管理器中没有显示任何表 有人能给我指点一下我应该去哪里或者哪里出了问题吗 > PM> update-database Specify the '-Verbose' f

我正在编写一个MVC web应用程序,并尝试使用代码优先迁移方法填充我的数据库

我在包管理器控制台上执行了以下命令

启用迁移-contexttypename IssueContext

添加迁移初始值创建

更新数据库

然后,我发现更新条目时出错。有关详细信息,请参见内部异常

我的代码没有错误,我的数据库已连接,但数据库资源管理器中没有显示任何表

有人能给我指点一下我应该去哪里或者哪里出了问题吗

> PM> update-database Specify the '-Verbose' flag to view the SQL
> statements being applied to the target database. No pending explicit
> migrations. Unable to update database to match the current model
> because there are pending changes and automatic migration is disabled.
> Either write the pending model changes to a code-based migration or
> enable automatic migration. Set
> DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable
> automatic migration. You can use the Add-Migration command to write
> the pending model changes to a code-based migration.

Configuration.cs(种子方法)

命名空间RecreationalServicesTicketingSystem.Migrations
{
使用制度;
使用System.Collections.Generic;
使用System.Data.Entity.Migrations;
使用System.Linq;
使用娱乐服务粘贴系统。模型;
内部密封类配置:DBMigOptionsConfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=假;
}
受保护的覆盖无效种子(RecreationalServicesTicketingSystem.DAL.IssueContext上下文)
{
var部门=新列表
{
新部门{DepartmentID=1,Name=“IT”},
新部门{DepartmentID=2,Name=“Admin”},
新部门{DepartmentID=3,Name=“人力资源”},
新部门{DepartmentID=4,Name=“Mechanics”},
新部门{DepartmentID=5,Name=“Directors”},
新部门{DepartmentID=6,Name=“Operations”}
};
departments.ForEach(s=>context.departments.AddOrUpdate(p=>p.Name,s));
SaveChanges();
var仓库=新列表
{
新仓库{DepotID=1,Name=“Porana”},
新仓库{DepotID=2,Name=“Far North”},
};
departments.ForEach(s=>context.departments.AddOrUpdate(p=>p.Name,s));
SaveChanges();
var users=新列表
{
新用户{FirstMidName=“Jason”,LastName=“Wan”,
EnrollmentDate=DateTime.Parse(“2016-02-18”)},
新用户{FirstMidName=“Andy”,LastName=“Domagas”,
EnrollmentDate=DateTime.Parse(“2016-02-18”)},
新用户{FirstMidName=“Denis”,LastName=“Djohar”,
EnrollmentDate=DateTime.Parse(“2016-02-18”)},
新用户{FirstMidName=“Christine”,LastName=“West”,
EnrollmentDate=DateTime.Parse(“2016-02-18”)},
};
users.ForEach(s=>context.users.AddOrUpdate(p=>p.LastName,s));
SaveChanges();
变量类别=新列表
{
新类别{CategoryID=0001,Title=“Desktop”},
新类别{CategoryID=0002,Title=“Mobile”},
新类别{CategoryID=0003,Title=“Menzits”},
新类别{CategoryID=0004,Title=“XMPRO”},
新类别{CategoryID=0005,Title=“Con-X”},
新类别{CategoryID=0006,Title=“Promapp”},
新类别{CategoryID=0007,Title=“QGIS”},
};
categories.ForEach(s=>context.categories.AddOrUpdate(p=>p.Title,s));
SaveChanges();
var tickets=新列表
{
新票{
UserID=users.Single(s=>s.LastName==“Wan”).UserID,
类别ID=类别。单个(c=>c.标题==“Con-X”)。类别ID,
问题=(“测试错误1”),
优先级=优先级。高
},
新票{
UserID=users.Single(s=>s.LastName==“Wan”).UserID,
CategoryID=类别。单个(c=>c.Title==“桌面”)。CategoryID,
问题=(“测试错误2”),
优先级=优先级.Med
},
};
foreach(票证中的票证e)
{
var ticketInDataBase=context.Tickets.Where(
s=>
s、 User.UserID==e.UserID&&
s、 Category.CategoryID==e.CategoryID).SingleOrDefault();
if(ticketInDataBase==null)
{
上下文。票证。添加(e);
}
}
SaveChanges();
var管理员=新列表
{
新管理员{AdminID=1,AdminRole=“Administrator LVL1”,User=users.Single(s=>s.UserID==1),
票证=新列表()},
新管理员{AdminID=2,AdminRole=“Administrator LVL2”,User=users.Single(s=>s.UserID==2),
票证=新列表()},
新管理员{AdminID=3,AdminRole=“Administrator LVL3”,User=users.Single(s=>s.UserID==3),
票证=新列表()}
};
administrator.ForEach(s=>context.Administrators.AddOrUpdate(p=>p.AdminID,s));
SaveChanges();
}
}
}
User.cs

public class User
{

    public int UserID { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public string FullName
    {
        get { return LastName + ", " + FirstMidName; }
    }
    public int AdministratorID { get; set; }
    [ForeignKey("AdministratorID")]
    public virtual Administrator Administrator { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }


    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }

    public int TicketID { get; set; }
    //Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
    public virtual ICollection<Ticket> Users { get; set; }

}
公共类用户
{
public int UserID{get;set;}
[StringLength(50,最小长度=1)]
公共字符串LastName{get;set;}
[StringLength(50,MinimumLength=1,ErrorMessage=“名字不能超过50个字符)。]
[列(“名字”)]
公共字符串FirstMidName{get;set;}
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:yyyy-MM-dd}”,ApplyFormatInEditMode=true)]
公共日期时间注册日期{get;set;}
普布利
public class User
{

    public int UserID { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public string FullName
    {
        get { return LastName + ", " + FirstMidName; }
    }
    public int AdministratorID { get; set; }
    [ForeignKey("AdministratorID")]
    public virtual Administrator Administrator { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }


    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }

    public int TicketID { get; set; }
    //Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
    public virtual ICollection<Ticket> Users { get; set; }

}
public class Ticket
    {
        public string Issue { get; set; }
        [DisplayFormat(NullDisplayText = "No Priority")]
        public Priority? Priority { get; set; }
        //Category One to Many Ticket
        public int CategoryID { get; set; }
        [ForeignKey("CategoryID")]
        public virtual Category Category { get; set; }
        //User (One to Many) Ticket
        public int UserID { get; set; }
        public int TicketID { get; set; }
        [ForeignKey("TicketID")]
        public virtual User User { get; set; }
        public int AdminID { get; set; }
        public virtual ICollection<Administrator> Administrators { get; set; }

    }
public class Depot
{
    //Do I need DepotID??
    public int DepotID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }

}
public class Department
{
    //Do I need DepartmentID?
    public int DepartmentID { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
}
public class Category
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CategoryID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Ticket> Tickets { get; set; }
}
public class Administrator
{
    [Key, ForeignKey("User")]
    public int UserID { get; set; }
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]
    public string AdminRole { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}
public int DepartmentID { get; set; }
[ForeignKey("DepartmentID")]
public virtual Department Department { get; set; } 
public int? DepartmentID { get; set; }
[ForeignKey("DepartmentID")]
public virtual Department Department { get; set; }