gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:51:20 26 4
gpt4 key购买 nike

我正在编写一个 MVC 网络应用程序,并尝试使用 Code First 迁移方法填充我的数据库。

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

启用迁移-contexttypename IssueContext

添加迁移 InitialCreate

更新数据库

然后我得到`更新条目时发生错误。有关详细信息,请参阅内部异常。

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

有人可以指点我应该看哪里或哪里出了问题吗?

> 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.

enter image description here

Configuration.cs(种子方法)

namespace RecreationalServicesTicketingSystem.Migrations
{
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using RecreationalServicesTicketingSystem.Models;

internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}

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

用户.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; }

}

Ticket.cs

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

}

仓库.cs

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

}

部门.cs

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

分类.cs

public class Category
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string Title { get; set; }

public virtual ICollection<Ticket> Tickets { get; set; }
}

管理员.cs

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

最佳答案

数据库抛出的错误是 FK_dbo.User_dbo.Department_DepartmentID 的外键违规。

您的用户有:

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

不可为 null 的 int 作为外键,这意味着它是必需的。

Seed 方法中创建 User 时,您没有设置 DepartmentId,因此违反了 FK。

因此您可以通过以下两种方式解决此问题:

Seed 方法中,您必须首先创建 DepartmentList,然后提供 DepartmentId创建属于该列表的 User。就像在创建 Ticket 时必须提供 UserIdCategoryId 一样。

您将 DepartmentId 创建为可为空:

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

关于c# - 代码没有错误,数据库已连接但更新条目时出错。有关详细信息,请参阅内部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com