C# 添加新类后迁移文件收缩
我不知道为什么我的迁移文件只有两行代码用于up和down方法,但我以前的一行有3个类,代码要多得多。如果我不正确地映射我的新类,会导致这个结果吗 我希望每个用户只有一个与他们相关的部门 我尝试执行更新数据库,但得到了错误代码(只是重要部分的一小部分) 更新条目时出错。查看内部异常 有关详细信息。-->System.Data.SqlClient.SqlException:插入 语句与外键约束冲突 “FK_dbo.User_dbo.Department_DepartmentID”。冲突发生在 数据库“RecreationalServicesTicketingSystem.DAL.IssueContext”,表 “副总裁部门”,列“部门ID” Configuration.cs包含5个类C# 添加新类后迁移文件收缩,c#,asp.net-mvc,entity-framework,relational-database,C#,Asp.net Mvc,Entity Framework,Relational Database,我不知道为什么我的迁移文件只有两行代码用于up和down方法,但我以前的一行有3个类,代码要多得多。如果我不正确地映射我的新类,会导致这个结果吗 我希望每个用户只有一个与他们相关的部门 我尝试执行更新数据库,但得到了错误代码(只是重要部分的一小部分) 更新条目时出错。查看内部异常 有关详细信息。-->System.Data.SqlClient.SqlException:插入 语句与外键约束冲突 “FK_dbo.User_dbo.Department_DepartmentID”。冲突发生在 数据库
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
// AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
{
var departments = new List<Department>
{
new Department { DepartmentID = 1, Name = "IT"},
new Department { DepartmentID = 2, Name = "Admin" },
new Department { DepartmentID = 3, Name = "Human Resources"},
new Department { DepartmentID = 4, Name = "Mechanics" },
new Department { DepartmentID = 5, Name = "Directors"},
new Department { DepartmentID = 6, Name = "Operations"}
};
departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name, s));
context.SaveChanges();
var depots = new List<Depot>
{
new Depot { DepotID = 1, Name = "Porana"},
new Depot { DepotID = 2, Name = "Far North"},
};
departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name, s));
context.SaveChanges();
var users = new List<User>
{
new User { FirstMidName = "Jason", LastName = "Wan",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Andy", LastName = "Domagas",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Denis", LastName = "Djohar",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Christine", LastName = "West",
EnrollmentDate = DateTime.Parse("2016-02-18") },
};
users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName, s));
context.SaveChanges();
var categories = new List<Category>
{
new Category {CategoryID = 0001, Title = "Desktop"},
new Category {CategoryID = 0002, Title = "Mobile"},
new Category {CategoryID = 0003, Title = "Menzits"},
new Category {CategoryID = 0004, Title = "XMPRO"},
new Category {CategoryID = 0005, Title = "Con-X"},
new Category {CategoryID = 0006, Title = "Promapp"},
new Category {CategoryID = 0007, Title = "QGIS"},
};
categories.ForEach(s => context.Categories.AddOrUpdate(p => p.Title, s));
context.SaveChanges();
var tickets = new List<Ticket>
{
new Ticket {
UserID = users.Single(s => s.LastName == "Wan").UserID,
CategoryID = categories.Single(c => c.Title == "Con-X" ).CategoryID,
Issue = ("Test Error 1"),
Priority = Priority.High
},
new Ticket {
UserID = users.Single(s => s.LastName == "Wan").UserID,
CategoryID = categories.Single(c => c.Title == "Desktop" ).CategoryID,
Issue = ("Test Error 2"),
Priority = Priority.Med
},
};
foreach (Ticket e in tickets)
{
var ticketInDataBase = context.Tickets.Where(
s =>
s.User.UserID == e.UserID &&
s.Category.CategoryID == e.CategoryID).SingleOrDefault();
if (ticketInDataBase == null)
{
context.Tickets.Add(e);
}
}
context.SaveChanges();
var administrator = new List<Administrator>
{
new Administrator {AdminID = 1, AdminRole = "Administrator LVL1", User = users.Single ( s => s.UserID == 1),
Tickets = new List<Ticket>() },
new Administrator {AdminID = 2, AdminRole = "Administrator LVL2", User = users.Single ( s => s.UserID == 2),
Tickets = new List<Ticket>() },
new Administrator {AdminID = 3, AdminRole = "Administrator LVL3", User = users.Single ( s => s.UserID == 3),
Tickets = new List<Ticket>() }
};
administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
context.SaveChanges();
}
}
public partial class InitialCreate : DbMigration
{
public override void Up()
{
DropColumn("dbo.Department", "UserID");
DropColumn("dbo.Depot", "UserID");
}
public override void Down()
{
AddColumn("dbo.Depot", "UserID", c => c.Int(nullable: false));
AddColumn("dbo.Department", "UserID", c => c.Int(nullable: false));
}
}
三类种子方法
namespace RecreationalServicesTicketingSystem.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.User",
c => new
{
UserID = c.Int(nullable: false, identity: true),
LastName = c.String(),
FirstMidName = c.String(),
EnrollmentDate = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.UserID);
CreateTable(
"dbo.Ticket",
c => new
{
TicketID = c.Int(nullable: false, identity: true),
CategoryID = c.Int(nullable: false),
UserID = c.Int(nullable: false),
Issue = c.String(),
Priority = c.Int(),
})
.PrimaryKey(t => t.TicketID)
.ForeignKey("dbo.Category", t => t.CategoryID, cascadeDelete: true)
.ForeignKey("dbo.User", t => t.UserID, cascadeDelete: true)
.Index(t => t.CategoryID)
.Index(t => t.UserID);
CreateTable(
"dbo.Category",
c => new
{
CategoryID = c.Int(nullable: false),
Title = c.String(),
})
.PrimaryKey(t => t.CategoryID);
}
public override void Down()
{
DropIndex("dbo.Ticket", new[] { "UserID" });
DropIndex("dbo.Ticket", new[] { "CategoryID" });
DropForeignKey("dbo.Ticket", "UserID", "dbo.User");
DropForeignKey("dbo.Ticket", "CategoryID", "dbo.Category");
DropTable("dbo.Category");
DropTable("dbo.Ticket");
DropTable("dbo.User");
}
}
}
科室
public class Department
{
public int DepartmentID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
公共课部
{
public int DepartmentID{get;set;}
[StringLength(50,最小长度=1)]
公共字符串名称{get;set;}
公共虚拟ICollection用户{get;set;}
}
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; }
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;}
公共字符串全名
{
获取{return LastName+“,”+FirstMidName;}
}
public int AdministratorID{get;set;}
[外键(“管理员ID”)]
公共虚拟管理员管理员{get;set;}
public int DepartmentID{get;set;}
[外键(“部门ID”)]
公共虚拟部门部门{get;set;}
public int DepotID{get;set;}
[外键(“DepotID”)]
公共虚拟仓库{get;set;}
public int TicketID{get;set;}
公共虚拟ICollection用户{get;set;}
}
您的部门ID和用户ID为int,因此对于SQL Server,它们将是标识列。如果没有“设置IDENTITY_insert[dbo].[Department]ON”或标记列以使其不会成为IDENTITY[DatabaseGenerated(DatabaseGeneratedOption.None)],则无法在Seed()方法中显式插入它们。您也可以在insert时排除Id并让SQL分配它
关于您的两次迁移,它们是相加的,而不是累积的。因此,第二次迁移将当前模型与上一次迁移进行比较,并产生差异