簡體   English   中英

實體框架未將所有信息保存到數據庫中

[英]Entity Framework not saving all the information into database

我正面臨一個問題,我嘗試了一些解決方案來解決該問題,但我做不到。 基本上,我有一個方法,我從ajax post接收一個字符串和一個序列化的Map作為參數。 與此相關的一切都運行良好,除了當我有相同的FileId和不同的UseCaseId 時,因為這樣只會保存一個信息,而不是同時保存兩者。

**UseCaseId 和 FileId ** 是來自其他表的外鍵。

關系:

modelBuilder.Entity<Content>()
        .HasIndex(ct => ct.FileId)
        .IsUnique(false);
    modelBuilder.Entity<Content>()
        .HasIndex(ct => ct.UseCaseId)
        .IsUnique(false);

    modelBuilder.Entity<Content>()
        .HasKey(cn => cn.ContentId);
    modelBuilder.Entity<Content>()
        .HasOne(cn => cn.File)
        .WithOne()
        .OnDelete(DeleteBehavior.Cascade);
    modelBuilder.Entity<Content>()
        .HasOne(cn => cn.UseCase)
        .WithOne()
        .OnDelete(DeleteBehavior.Cascade);

在以下示例中,需要保存兩個完全相同的元素,只是UseCase不同,但數據庫中只保存最后一個。

內容變量

數據庫 - 結果

收到的參數:

收到的參數

這是出現問題的代碼:

[HttpPost]
public JsonResult Create([FromBody] ContentJson data)
{
    List<Content> contents = new List<Content>();
    var contentName = string.Empty;
    var array = data.content;
    int order;

    for (int i = 0; i < array.Count; i++)
    {
        var files = JsonConvert.DeserializeObject<string[]>(array[i][1].ToString());

        if (files == null || files.Length == 0)
            continue;

        contentName = array[i][0].ToString();
        order = 1;

        foreach(var fileName in files)
        {
            Content content = new Content();    
            content.Order = order;
            content.UseCaseId = long.Parse(contentName);
            content.FileId = unitOfWork.FileRepository.Get(f => f.FileName.Equals(fileName)).First().FileId;
            contents.Add(content);
            order++;
        }
    }

    unitOfWork.ContentRepository.AddRange(contents);
    unitOfWork.Save();

    return Json(Ok());
}
  • ContentId是自動生成的。
  • 最新版本的 EntityFramework 和 Dotnet
  • 我已經嘗試在每次迭代中保存。
  • 嘗試實現 Equals 和 HashCode,

     public void Save() { context.SaveChanges(); } private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { context.Dispose(); } } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public Repository<Content> ContentRepository { get { if(contentRepository == null) { contentRepository = new Repository<Content>(context); } return contentRepository; } }

有人可以幫我嗎? 我被這個問題困住了。

您有沖突的 FK 配置。

IsUnique(false)在這里

modelBuilder.Entity<Content>()
    .HasIndex(ct => ct.FileId)
    .IsUnique(false);

modelBuilder.Entity<Content>()
    .HasIndex(ct => ct.UseCaseId)
    .IsUnique(false);

暗示 FK 不是唯一的,但是這里的WithOne()

modelBuilder.Entity<Content>()
    .HasOne(cn => cn.File)
    .WithOne()
    .OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Content>()
    .HasOne(cn => cn.UseCase)
    .WithOne()
    .OnDelete(DeleteBehavior.Cascade);

意味着它們是唯一的(關系數據庫中一對多和一對一 FK 關系之間的唯一區別是后面的 FK 支持唯一約束/索引)。 這優先於非唯一索引映射並導致問題。

由於您希望它們不是唯一的,因此您必須通過在兩個地方將WithOne()替換為WithOne()來將關系更改為一對多(或多對一,具體取決於您所看的一側WithMany() .

執行此操作后,您可以刪除兩個HasIndex配置,因為它們將根據 EF Core 約定自動創建。 事實上,它們也可以使用您的流暢配置創建,但是是獨一無二的。 可能這就是您試圖用這些HasIndex語句修復的問題,但這只是錯誤的地方,而您應該修復導致它的原因。

代替 contentName 使用 fileName.contentName
即 content.UseCaseId = long.Parse(fileName.contentName); 同樣適用於 fileId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM