一旦我嘗試洗掉其中一個,我試圖獲得一定數量的評論最多的動物,所以我收到以下錯誤:SqlException:DELETE 陳述句與 REFERENCE 約束“FK__Comments__Animal__2EDAF651”沖突。沖突發生在資料庫“PetShop”、表“dbo.Comments”、列“AnimalId”中。該陳述句已終止。我想讓它成為可能,如果我洗掉,那么你將繼續前進到下一行
我的顯示控制器:
public async Task<IActionResult> Index()
{
var animal = _context.Animals.Include(c => c.Comments).OrderByDescending(c => c.Comments.Count).Take(2);
return View(await animal.ToListAsync());
}
我的洗掉控制器:
public async Task<Animal> DeleteAnimal(int id)
{
var comment = await _context.Comments.FindAsync(id);
_context.Comments.Remove(comment!);
var animal = await _context.Animals.FindAsync(id);
_context.Animals.Remove(animal!);
await _context.SaveChangesAsync();
return animal!;
}
我的背景:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Comment>(entity =>
{
entity.HasOne(d => d.Animal)
.WithMany(p => p.Comments)
.HasForeignKey(d => d.AnimalId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__Comments__Animal__2EDAF651");
});
modelBuilder.Entity<Category>(entity =>
entity.HasData(
new { CategoryId = 1, Name = "Dogs" },
new { CategoryId = 2, Name = "Cats" },
new { CategoryId = 3, Name = "Birds" },
new { CategoryId = 4, Name = "Rabbits" },
new { CategoryId = 5, Name = "Hamsters" }
)
);
modelBuilder.Entity<Animal>(entity =>
{
entity.HasData(
new { AnimalId = 1, Name = "Shoko", BirthDate = DateTime.Now.AddYears(-1).AddMonths(-1).AddDays(-12), Description = "Friendly and loyal", CategoryId = 1, PhotoUrl = "ShokoDog.jpg" },
new { AnimalId = 2, Name = "Bamba", BirthDate = DateTime.Now.AddYears(-2).AddMonths(-2).AddDays(-3), Description = "Furry and neutered", CategoryId = 2, PhotoUrl = "BambaCat.jpg" },
new { AnimalId = 3, Name = "Regev", BirthDate = DateTime.Now.AddYears(-1).AddMonths(-3).AddDays(-3), Description = "Speak", CategoryId = 3, PhotoUrl = "RegevBird.jpg" },
new { AnimalId = 4, Name = "Humi", BirthDate = DateTime.Now.AddYears(-3).AddMonths(-4).AddDays(-7), Description = "Cute and furry", CategoryId = 4, PhotoUrl = "HumiRabbit.jpg" },
new { AnimalId = 5, Name = "Tommy", BirthDate = DateTime.Now.AddYears(-1).AddMonths(-7).AddDays(-9), Description = "Love to play in the facilities", CategoryId = 5, PhotoUrl = "TommyHamster.jpg" });
});
OnModelCreatingPartial(modelBuilder);
}
uj5u.com熱心網友回復:
您要洗掉父動物和相關子評論
要擴展 Karlis 的建議:
- 您有問題中發布的模型和背景關系(這有點問題,因為您對 EF 說設定 null,但代碼和 DB 不會接受 null)但您可以這樣做:
var a = context.Animals.Include(a => a.Comments).Find(id):
context.Comments.RemoveRange(a.Comments);
context.Animals.Remove(a);
context.SaveChanges();
這明確洗掉了評論,然后是動物
- 更改要使用的背景關系,
.OnDelete(DeleteBehavior.ClientCascade)然后您可以執行以下操作:
var a = context.Animals.Include(a => a.Comments).Find(id):
context.Animals.Remove(a);
context.SaveChanges();
這會導致 EF 在您明確告訴它洗掉動物時隱式洗掉??它知道的注釋
- 更改資料庫的外鍵以執行 ON DELETE CASCADE,然后更改
.OnDelete(DeleteBehavior.Cascade)然后您可以跳過下載評論(不包括):
var a = context.Animals.Find(id):
context.Animals.Remove(a);
context.SaveChanges();
這會導致資料庫在 EF 指示洗掉動物時洗掉評論(EF 不知道它們)
從廣義上講,這些是按照“你會犯多嚴重的錯誤”從“不太”到“相當多”的順序
uj5u.com熱心網友回復:
錯誤訊息顯示您正在洗掉 Animal,它具有關聯的評論。您應該執行以下操作之一:
- 在洗掉動物之前洗掉與特定動物相關的評論。
- 檢查 EF 配置以在洗掉時進行級聯
- 更改 FK 以在洗掉時進行級聯(這取決于您使用的是資料庫優先還是代碼優先方法)
我會選擇第一種方法,因為洗掉時的級聯可能很危險,并且會默默地洗掉無意參考的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/463228.html
標籤:asp.net-mvc 林克 语法错误
上一篇:.xxxxxx??v.Outer.Outer.xxxx)'無法用空條件asp.netcore6翻譯OrderByDescending
