为什么 entity framework 试图删除删除时的关联行?
Why is entity framework trying to delete the associated row on delete?
所以我得到了这个代码:
...
modelBuilder.Entity<Person>(builder => {
builder.ToTable("person");
builder.Property(x => x.PersonId).HasColumnName("nconst");
builder.Property(x => x.FirstName).HasColumnName("firstname");
builder.Property(x => x.LastName).HasColumnName("lastname");
builder.Property(x => x.BirthYear).HasColumnName("birthyear");
builder.HasKey(x => x.PersonId);
builder.HasMany(x => x.Works).WithOne().HasForeignKey(x => x.PersonId);
builder.HasOne(x => x.Death).WithOne().HasForeignKey<Death>(x => x.PersonId);
});
modelBuilder.Entity<PersonBookmark>(builder => {
builder.ToTable("personbookmark");
builder.Property(x => x.PersonId).HasColumnName("nconst");
builder.Property(x => x.UserId).HasColumnName("user_id");
builder.Property(x => x.Date).HasColumnName("date");
builder.Property(x => x.Label).HasColumnName("label");
builder.HasKey(x => new { x.UserId, x.PersonId });
builder.HasOne(x => x.User).WithMany(x => x.PersonBookmarks).HasForeignKey(x => x.UserId);
builder.HasOne(x => x.Person).WithOne().HasPrincipalKey<PersonBookmark>(x => x.PersonId).HasForeignKey<Person>(x => x.PersonId);
});
...
namespace DataService.Domain {
public class PersonBookmark {
public int UserId { get; set; }
public DateTime Date { get; set; }
[StringLength(100, MinimumLength = 1)]
public string Label { get; set; }
[StringLength(10, MinimumLength = 10)]
public string PersonId { get; set; }
[NotMapped]
public virtual User User { get; set; }
[NotMapped]
public virtual Person Person { get; set; }
}
}
namespace DataService.Domain {
public class Person {
public Person() {
Works = new HashSet<Work>();
}
[StringLength(10, MinimumLength = 10)]
public string PersonId { get; set; }
[StringLength(100, MinimumLength = 1)]
public string LastName { get; set; }
[StringLength(100, MinimumLength = 1)]
public string FirstName { get; set; }
[StringLength(4, MinimumLength = 4)]
public string BirthYear { get; set; }
[NotMapped]
public virtual ICollection<Work> Works { get; set; }
[NotMapped]
public virtual Death Death { get; set; }
}
}
public bool DeletePersonBookmark(int UserId, string PersonId) {
var personBookmark = Context.PersonBookmarks.FirstOrDefault(x => x.UserId == UserId && x.PersonId == PersonId);
var found = personBookmark != null;
if (found) {
Context.PersonBookmarks.Remove(personBookmark);
Context.SaveChanges();
}
return found;
}
当我试图从数据库中删除 PersonBookmark
时,我遇到了这个异常:
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- Npgsql.PostgresException : 23503: update or delete on table "person" violates foreign key constraint "personbookmark_nconst_fkey" on table "personbookmark"
看起来 entity framework 正试图删除关联的 Person
行,但我不知道我在哪里告诉他这样做...
我只想删除 PersonBookmark
行。也许我不明白一些明显的事情,但在我看来 Person
影子 属性 并不意味着关联的 Person
应该与其引用所有者一起消失。
有人可以帮助我吗?
提前致谢
It looks like entity framework is trying to remove the associated Person row, but I don't know where I told him to do this...
就在这里
builder.HasOne(x => x.Person).WithOne()
.HasPrincipalKey<PersonBookmark>(x => x.PersonId) // <--
.HasForeignKey<Person>(x => x.PersonId);
对于一对一关系,HasPrincipalKey
/ HasForeignKey
的通用类型参数用于指定谁是 principal 以及谁是依赖关系 (docs)。并且级联删除从主体到依赖方向工作,即删除主体级联删除依赖项。
这里需要的是交换Person
和PersonBookmark
角色:
builder.HasOne(x => x.Person).WithOne()
.HasPrincipalKey<Person>(x => x.PersonId)
.HasForeignKey<PersonBookmark>(x => x.PersonId);
确保重新生成迁移并将其应用到数据库。
所以我得到了这个代码:
...
modelBuilder.Entity<Person>(builder => {
builder.ToTable("person");
builder.Property(x => x.PersonId).HasColumnName("nconst");
builder.Property(x => x.FirstName).HasColumnName("firstname");
builder.Property(x => x.LastName).HasColumnName("lastname");
builder.Property(x => x.BirthYear).HasColumnName("birthyear");
builder.HasKey(x => x.PersonId);
builder.HasMany(x => x.Works).WithOne().HasForeignKey(x => x.PersonId);
builder.HasOne(x => x.Death).WithOne().HasForeignKey<Death>(x => x.PersonId);
});
modelBuilder.Entity<PersonBookmark>(builder => {
builder.ToTable("personbookmark");
builder.Property(x => x.PersonId).HasColumnName("nconst");
builder.Property(x => x.UserId).HasColumnName("user_id");
builder.Property(x => x.Date).HasColumnName("date");
builder.Property(x => x.Label).HasColumnName("label");
builder.HasKey(x => new { x.UserId, x.PersonId });
builder.HasOne(x => x.User).WithMany(x => x.PersonBookmarks).HasForeignKey(x => x.UserId);
builder.HasOne(x => x.Person).WithOne().HasPrincipalKey<PersonBookmark>(x => x.PersonId).HasForeignKey<Person>(x => x.PersonId);
});
...
namespace DataService.Domain {
public class PersonBookmark {
public int UserId { get; set; }
public DateTime Date { get; set; }
[StringLength(100, MinimumLength = 1)]
public string Label { get; set; }
[StringLength(10, MinimumLength = 10)]
public string PersonId { get; set; }
[NotMapped]
public virtual User User { get; set; }
[NotMapped]
public virtual Person Person { get; set; }
}
}
namespace DataService.Domain {
public class Person {
public Person() {
Works = new HashSet<Work>();
}
[StringLength(10, MinimumLength = 10)]
public string PersonId { get; set; }
[StringLength(100, MinimumLength = 1)]
public string LastName { get; set; }
[StringLength(100, MinimumLength = 1)]
public string FirstName { get; set; }
[StringLength(4, MinimumLength = 4)]
public string BirthYear { get; set; }
[NotMapped]
public virtual ICollection<Work> Works { get; set; }
[NotMapped]
public virtual Death Death { get; set; }
}
}
public bool DeletePersonBookmark(int UserId, string PersonId) {
var personBookmark = Context.PersonBookmarks.FirstOrDefault(x => x.UserId == UserId && x.PersonId == PersonId);
var found = personBookmark != null;
if (found) {
Context.PersonBookmarks.Remove(personBookmark);
Context.SaveChanges();
}
return found;
}
当我试图从数据库中删除 PersonBookmark
时,我遇到了这个异常:
Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
---- Npgsql.PostgresException : 23503: update or delete on table "person" violates foreign key constraint "personbookmark_nconst_fkey" on table "personbookmark"
看起来 entity framework 正试图删除关联的 Person
行,但我不知道我在哪里告诉他这样做...
我只想删除 PersonBookmark
行。也许我不明白一些明显的事情,但在我看来 Person
影子 属性 并不意味着关联的 Person
应该与其引用所有者一起消失。
有人可以帮助我吗?
提前致谢
It looks like entity framework is trying to remove the associated Person row, but I don't know where I told him to do this...
就在这里
builder.HasOne(x => x.Person).WithOne()
.HasPrincipalKey<PersonBookmark>(x => x.PersonId) // <--
.HasForeignKey<Person>(x => x.PersonId);
对于一对一关系,HasPrincipalKey
/ HasForeignKey
的通用类型参数用于指定谁是 principal 以及谁是依赖关系 (docs)。并且级联删除从主体到依赖方向工作,即删除主体级联删除依赖项。
这里需要的是交换Person
和PersonBookmark
角色:
builder.HasOne(x => x.Person).WithOne()
.HasPrincipalKey<Person>(x => x.PersonId)
.HasForeignKey<PersonBookmark>(x => x.PersonId);
确保重新生成迁移并将其应用到数据库。