gpt4 book ai didi

c# - 在 Entity Framework 中存储自定义对象列表

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:49 24 4
gpt4 key购买 nike

开始学习asp.net和DB操作。尝试实现一些简单的功能 - 两个模型,一个具有对另一个的引用列表。

这是我目前遇到的一个错误:

An exception occurred while initializing the database. See the InnerException for details.

Inner exception:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

我的模型:

public class Killer
{
public Killer(string name, string biography)
{
Name = name;
Biography = biography;
KillerId = Guid.NewGuid();
}

public Guid KillerId { get; set; }
public string Name { get; set; }
public string Biography { get; set; }

public virtual Contract Contract { get; set; }
}

public class Contract
{
public Contract(Status status, Killer target, string description, params Killer[] targets)
{
ContractId = Guid.NewGuid();
this.status = status;
Target = target;
Description = description;

Killers = new HashSet<Killer>();
foreach (var t in targets) Killers.Add(t);

}

public Guid ContractId { get; set; }

public enum Status { active, done, failed, rejected, abandoned }
public Status status { get; set; }

public Killer Target { get; set; }
public string Description { get; set; }

//[ForeignKey("ContractID")]
public virtual ICollection<Killer> Killers { get; set; }
}

在上下文中我用对象列表初始化数据库

public class KillerContext : DbContext
{
public DbSet<Killer> Killers { get; set; }
public DbSet<Contract> Contracts { get; set; }
}

在 Controller 中我这样做:

KillerContext k = new KillerContext();

public ActionResult Index()
{
var contracts = k.Contracts.ToList();
ViewBag.contracts = contracts;
return View();
}

Global.asax 中:

Database.SetInitializer(new KillerContextInitialization());

这是我在数据库中输入第一个数据的方式:

public sealed class KillerContextInitialization : DropCreateDatabaseAlways<KillerContext>
{
protected override void Seed(KillerContext db)
{
List<Killer> killers = new List<Killer>();
//List<Contract> contracts = new List<Contract>();

killers.Add(new Killer(name: "Ivan Firstein", biography: "He was born in the shadows."));
killers.Add(new Killer(name: "Oleg Gazmanov", biography: "test man"));
db.Contracts.Add(new Contract(
Contract.Status.active,
killers.SingleOrDefault(x => x.Name == "Ivan Firstein"),
"KILL OR BE KILLED. As always with love.",
killers.SingleOrDefault(x => x.Name == "Oleg Gazmanov")
));

db.Killers.AddRange(killers);
base.Seed(db);
}
}

最佳答案

看起来您需要为 killer 模型添加 ForeignKey 属性,并将此键存储在属性 ContractId 中:

public class Killer
{
[ForeignKey(nameof(ContractId)] //Name of added property in line below
public Contract Contract { get; set; } //no need "virtual"
public Guid? ContractId { get; set; }

// other properties...
}

public class Contract
{
[ForeignKey("ContractId")] //Name of added property in Killer Model
public virtual ICollection<Killer> Killers { get; set; }

// other code...
}

编辑

您应该执行类似于 Contract.Target 属性的操作:

[ForeignKey(nameof(TargetId)]
public Killer Target { get; set; }
public Guid TargetId { get; set; }

对于枚举类型,你应该像这样添加属性:

[Column(nameof(status), TypeName = "int")]
public Status status { get; set; }

关于c# - 在 Entity Framework 中存储自定义对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47353028/

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