C# Entity Framework 链表外键
C# Entity Framework LinkedList ForeignKeys
我想在数据库中保存一个 LinkedList 以便稍后处理,因此我得到了一个包含一个字段“Next”和一个字段“Previous”的模型。对于“create table”语句,它应该创建两个名为 PreviousId 和 NextId 的外键,但是当我在 SQLite Db 中看到 create 语句时,只有一个。
我已将参考添加到 OnModelcreating 方法,但似乎第二个被解释为第一个的重复。
我有以下模型要存储在数据库中:
public class BlockLink : BaseEntry
{
[ForeignKey(nameof(PreviousId))]
public ulong? PreviousId { get; set; }
[ForeignKey(nameof(NextId))]
public ulong? NextId { get; set; }
public BlockLink Previous { get; set; }
public int Current { get; set; }
public BlockLink Next { get; set; }
public Targets Target { get; set; }
public BlockLink(Targets target) => Target = target;
}
这里是 OnModelCreating 方法中的关系:
modelBuilder.Entity<BlockLink>()
.HasOne(f => f.Next)
.WithOne(f => f.Previous)
.HasForeignKey(typeof(BlockLink), nameof(BlockLink.NextId));
modelBuilder.Entity<BlockLink>()
.HasOne(f => f.Previous)
.WithOne(f => f.Next)
.HasForeignKey(typeof(BlockLink), nameof(BlockLink.PreviousId));
这是创建语句:
CREATE TABLE "BlockList" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_BlockList" PRIMARY KEY AUTOINCREMENT,
"PreviousId" INTEGER NULL,
"NextId" INTEGER NULL,
"Current" INTEGER NULL,
"Target" INTEGER NOT NULL,
"CreationTime" TEXT NOT NULL,
"LastUpdate" TEXT NOT NULL,
"Info" TEXT NULL,
CONSTRAINT "FK_BlockList_BlockList_PreviousId" FOREIGN KEY ("PreviousId") REFERENCES "BlockList" ("Id") ON DELETE RESTRICT)
我发现,其实并没有那么难。
解决方案:
modelBuilder.Entity<BlockLink>()
.HasOne(x => x.Next);
modelBuilder.Entity<BlockLink>()
.HasOne(n => n.Previous);
public class BlockLink : BaseEntry
{
public ulong? PreviousId { get; set; }
public ulong? NextId { get; set; }
[ForeignKey(nameof(PreviousId))]
public BlockLink Previous { get; set; }
public TX_BASE_Type Current { get; set; }
[ForeignKey(nameof(NextId))]
public BlockLink Next { get; set; }
public Targets Target { get; set; }
public BlockLink(Targets target) => Target = target;
}
我想在数据库中保存一个 LinkedList 以便稍后处理,因此我得到了一个包含一个字段“Next”和一个字段“Previous”的模型。对于“create table”语句,它应该创建两个名为 PreviousId 和 NextId 的外键,但是当我在 SQLite Db 中看到 create 语句时,只有一个。
我已将参考添加到 OnModelcreating 方法,但似乎第二个被解释为第一个的重复。
我有以下模型要存储在数据库中:
public class BlockLink : BaseEntry
{
[ForeignKey(nameof(PreviousId))]
public ulong? PreviousId { get; set; }
[ForeignKey(nameof(NextId))]
public ulong? NextId { get; set; }
public BlockLink Previous { get; set; }
public int Current { get; set; }
public BlockLink Next { get; set; }
public Targets Target { get; set; }
public BlockLink(Targets target) => Target = target;
}
这里是 OnModelCreating 方法中的关系:
modelBuilder.Entity<BlockLink>()
.HasOne(f => f.Next)
.WithOne(f => f.Previous)
.HasForeignKey(typeof(BlockLink), nameof(BlockLink.NextId));
modelBuilder.Entity<BlockLink>()
.HasOne(f => f.Previous)
.WithOne(f => f.Next)
.HasForeignKey(typeof(BlockLink), nameof(BlockLink.PreviousId));
这是创建语句:
CREATE TABLE "BlockList" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_BlockList" PRIMARY KEY AUTOINCREMENT,
"PreviousId" INTEGER NULL,
"NextId" INTEGER NULL,
"Current" INTEGER NULL,
"Target" INTEGER NOT NULL,
"CreationTime" TEXT NOT NULL,
"LastUpdate" TEXT NOT NULL,
"Info" TEXT NULL,
CONSTRAINT "FK_BlockList_BlockList_PreviousId" FOREIGN KEY ("PreviousId") REFERENCES "BlockList" ("Id") ON DELETE RESTRICT)
我发现,其实并没有那么难。 解决方案:
modelBuilder.Entity<BlockLink>()
.HasOne(x => x.Next);
modelBuilder.Entity<BlockLink>()
.HasOne(n => n.Previous);
public class BlockLink : BaseEntry
{
public ulong? PreviousId { get; set; }
public ulong? NextId { get; set; }
[ForeignKey(nameof(PreviousId))]
public BlockLink Previous { get; set; }
public TX_BASE_Type Current { get; set; }
[ForeignKey(nameof(NextId))]
public BlockLink Next { get; set; }
public Targets Target { get; set; }
public BlockLink(Targets target) => Target = target;
}