gpt4 book ai didi

entity-framework - Entity Framework Core 自引用表

转载 作者:行者123 更新时间:2023-12-04 01:51:13 24 4
gpt4 key购买 nike

我有一个类(class)叫 Item它引用下一项和上一项。

public class Item
{
private Item() { }

public Item(string itemName)
{
ItemId = Guid.NewGuid();
ItemName = itemName;
}

public Guid ItemId { get; set; }
public string ItemName { get; set; }

public Guid NextItemId { get; set; }
public virtual Item NextItem { get; set; }

public Guid PreviousItemId { get; set; }
public virtual Item PreviousItem { get; set; }

public Guid GroupId { get; set; }
public virtual Group Group { get; set; }
}

我还有一张 table 叫 Group它用于对项目进行分组。
public class Group
{
private Group() { }
public Group(string groupName)
{
GroupId = Guid.NewGuid();
GroupName = groupName;
GroupItems = new List<Item>();
}

public void AddGroupItem(Item item)
{
if (Items.Count == 0)
{
Items.Add(item);
}
else
{
item.PreviousItem = Items.Last();
item.PreviousItemId = Items.Last().ItemId;
Items.Last().NextItem = item;
Items.Last().NextItemId = item.ItemId;

Items.Add(item);
}

}

public Guid GroupId { get; set; }
public string GroupName { get; set; }
public virtual IList<GroupItem> GroupItems { get; set; }
}

这是我创建和保存项目及其组的方法。
Group group1 = new Group("first group");
Item item1 = new Item("item 1");
Item item2 = new Item("item 2");
Item item3 = new Item("item 3");

group1.AddItem(item1);
group1.AddItem(item2);
group1.AddItem(item3);

_context.Add(group1);
_context.SaveChanges();

我该怎么写 OnModelCreating处理对同一个表的两个引用。

最佳答案

你可以用下一个方法来做。首先,您应该为模型 public virtual List<Item> ParentNextItems { get; set; } 添加两个新属性和 public virtual List<Item> ParentPreviousItems { get; set; } .所以你的模型将是这样的

public class Item
{
private Item() { }

public Item(string itemName)
{
ItemId = Guid.NewGuid();
ItemName = itemName;
}

public Guid ItemId { get; set; }
public string ItemName { get; set; }

public Guid? NextItemId { get; set; }
public virtual Item NextItem { get; set; }
public virtual List<Item> ParentNextItems { get; set; }

public Guid? PreviousItemId { get; set; }
public virtual Item PreviousItem { get; set; }
public virtual List<Item> ParentPreviousItems { get; set; }
}

然后你可以用下一种方式配置它
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
.HasKey(x => x.ItemId);
modelBuilder.Entity<Item>()
.HasOne(x => x.NextItem).WithMany(x => x.ParentNextItems).HasForeignKey(x => x.NextItemId)
.Metadata.DeleteBehavior = DeleteBehavior.Restrict;
modelBuilder.Entity<Item>()
.HasOne(x => x.PreviousItem).WithMany(x => x.ParentPreviousItems).HasForeignKey(x => x.PreviousItemId)
.Metadata.DeleteBehavior = DeleteBehavior.Restrict;


base.OnModelCreating(modelBuilder);
}

就这样。但是如果你想用属性配置来达到同样的效果,那么你可以跳过 void OnModelCreating(ModelBuilder modelBuilder)更改并编写下一个模型:
public class Item
{
private Item() { }

public Item(string itemName)
{
ItemId = Guid.NewGuid();
ItemName = itemName;
}

[Key]
public Guid ItemId { get; set; }
public string ItemName { get; set; }

public Guid? NextItemId { get; set; }

[ForeignKey(nameof(NextItemId))]
[InverseProperty(nameof(ParentNextItems))]
public virtual Item NextItem { get; set; }

[ForeignKey(nameof(NextItemId))]
public virtual List<Item> ParentNextItems { get; set; }

public Guid? PreviousItemId { get; set; }
[ForeignKey(nameof(PreviousItemId))]
[InverseProperty(nameof(ParentPreviousItems))]
public virtual Item PreviousItem { get; set; }
[ForeignKey(nameof(PreviousItemId))]
public virtual List<Item> ParentPreviousItems { get; set; }
}

更新
此外,您应该将 PreviousItemId、NextItemId 设为可选(Guid?),我在答案中做了相应的更改。

据我所知,你不能一次性完成 .SaveChanges()旅行。创建第二个项目时,您应该已经将第一个项目保存到数据库中。 (无论如何,这是另一个问题的主题)

但是无论如何,如果您将代码修改为类似的内容
Group group1 = new Group("first group");
_context.Add(group1);
Item item1 = new Item("item 1");
group1.AddItem(item1);
_context.SaveChanges();
Item item2 = new Item("item 2");
group1.AddItem(item2);
_context.SaveChanges();
Item item3 = new Item("item 3");
group1.AddItem(item3);
_context.SaveChanges();

一机搞定 transaction

关于entity-framework - Entity Framework Core 自引用表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927982/

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