簡體   English   中英

實體框架 - 如果尚未持久化,則避免子重復

[英]Entity Framework - Avoiding child duplicates if they are not persited yet

實際上我使用的是 DbContext,但我也只是使用 ObjectContext 對其進行了測試。

// Engine 1
Engine engine = new Engine();
Manufacturer manufacturer = new Manufacturer();

engine.Manufacturer = manufacturer;

// Engine 2
engine = new Engine();
engine.Manufacturer = manufacturer // Engine 2 has the same manufacturer like Engine 1

context.SaveChanges();

我使用標識列 (int),在其中生成新的 ID。 在調試模式下,我看到引擎的 ID 為“0”。 好吧,如果我在 Engine 1 塊之后立即實施 context.SaveChanges,則新制造商將被保存到 DB。 使用 EntityKey 或 Any 檢查,我可以毫無問題地將新制造商引用到 Engine 2。 但是如果沒有立即 SaveChanges(),同一制造商的兩個條目將被保存到 DB(上面的代碼)。 EF 是否無法像普通對象一樣在內部引用? 正如您在上面看到的,制造商是同一個 object,所以我想知道是否有可能在不預先保存子/制造商的情況下獲得成功的插入。

編輯:我想我發現了問題

MachineEntities context = new MachineEntities();
        context.Configuration.AutoDetectChangesEnabled = true;

        // Engine 1
        Engine engine1 = new Engine();
        engine1.Name = "engine1";

        Manufacturer manufacturer = new Manufacturer();
        manufacturer.Name = "manufacturer1";

        engine1.Manufacturer = manufacturer;

        // Engine 2
        Engine engine2 = new Engine();
        engine2.Name = "engine2";

        manufacturer = new Manufacturer();
        manufacturer.Name = "manufacturer1";

        engine2.Manufacturer = manufacturer;


        // Add Engine 1

        if (context.Manufacturers.Any(m => m.Name == engine1.Manufacturer.Name))
        {
            // The manufacturer's name is identical, so use the one in the context instead the assigned one.
            engine1.Manufacturer = context.Manufacturers.Single(m => m.Name == engine1.Manufacturer.Name);
        }
        else
        {
            // The manufacturer is not known, add it to the context
            context.Set<Manufacturer>().Add(engine1.Manufacturer);
        }

        // Add Engine 2


        if (context.Manufacturers.Any(m => m.Name == engine1.Manufacturer.Name))
        {
            // The manufacturer's name is identical, so use the one in the context instead the assigned one.
            engine2.Manufacturer = context.Manufacturers.Single(m => m.Name == engine2.Manufacturer.Name);
        }
        else
        {
            context.Manufacturers.Add(engine2.Manufacturer);
        }

        context.SaveChanges();

        context.Dispose();

“任何”或任何比較都不會給我任何結果。 它只給我那些已經保存在數據庫中的實體,而不是新添加的實體。 所以它是重復的。 正如我在調試器中看到的那樣,本地的被忽略,而“結果視圖”中的那個是執行命令的那個。 所以新添加的實體位於“Manufacturers.Local”中。

我剛剛嘗試了以下方法:

var a1 = new Activity{WorkflowId = 1, Title = "test"};
var a2 = new Activity{WorkflowId = 1, Title = "test2"};
var d = new WorkflowDisplay{WorkflowId = 1, Title = "Test"};
a1.WorkflowDisplay = d;
a2.WorkflowDisplay = d;
// Any of the next three lines can be commented out, but if
// they are all commented, nothing happens.
AddToWorkflowDisplays(d);
AddToActivities(a1);
AddToActivities(a2);
SaveChanges();

...我只看到添加了一個WorkflowDisplay 所以我很確定這與您的具體實現有關。 您是否覆蓋了任何實體的GetHashCodeEquals方法,或者對自動生成的代碼進行了任何類似的自定義?

暫無
暫無

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

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