所以我有這個代碼:
...
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從資料庫中洗掉 a 時,我遇到了這個例外:
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"
看起來物體框架正在嘗試洗掉關聯的Person行,但我不知道我在哪里告訴他這樣做的......我只想PersonBookmark洗掉該行。也許我不明白一些明顯的東西,但在我看來,Person影子屬性并不意味著關聯Person應該與其參考所有者一起消失。
有人能幫我嗎?
先感謝您
uj5u.com熱心網友回復:
看起來物體框架正試圖洗掉關聯的 Person 行,但我不知道我在哪里告訴他這樣做的......
就在這兒
builder.HasOne(x => x.Person).WithOne()
.HasPrincipalKey<PersonBookmark>(x => x.PersonId) // <--
.HasForeignKey<Person>(x => x.PersonId);
對于一對一關系,HasPrincipalKey/的泛型型別引數HasForeignKey用于指定關系中誰是主體,誰是從屬關系 ( docs )。級聯洗掉從主體到依賴方向,即洗掉主體級聯洗掉依賴。
你在這里需要的是交換Person和PersonBookmark角色:
builder.HasOne(x => x.Person).WithOne()
.HasPrincipalKey<Person>(x => x.PersonId)
.HasForeignKey<PersonBookmark>(x => x.PersonId);
確保重新生成遷移并將其應用到資料庫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359183.html
