I'm going to create a new cascade [node] --1:many-- [currency] --1:many-- [unit] defined using EF 6.x Code First:
public class CashFlowSpendableMinimumNode {
...
[ForeignKey("CashFlowSpendableMinimumNodeId")]
public ICollection<CashFlowSpendableMinimumCurrency> CashFlowSpendableMinimumCurrencies { get; private set; }
...
}
public class CashFlowSpendableMinimumCurrency {
...
public int CashFlowSpendableMinimumNodeId { get; set; }
[ForeignKey("CashFlowSpendableMinimumNodeId")]
public CashFlowSpendableMinimumNode CashFlowSpendableMinimumNode { get; set;
...
[ForeignKey("CashFlowSpendableMinimumCurrencyId")]
public ICollection<CashFlowSpendableMinimumUnit> CashFlowSpendableMinimumUnits { get; set; }
...
}
public class CashFlowSpendableMinimumUnit {
...
public int CashFlowSpendableMinimumCurrencyId { get; set; }
[ForeignKey("CashFlowSpendableMinimumCurrencyId")]
public CashFlowSpendableMinimumCurrency CashFlowSpendableMinimumCurrency { get; set; }
...
}
The first part i.e. relation between node--currency work as expected: I create a new currency instance and add it to DbSet<CashFlowSpendableMinimumCurrency> -> it appears in the navigation property (collection) nodeMinimum.CashFlowSpendableMinimumCurrencies and its Id is resolved properly when relation is saved to DB.
The second relation currency--unit does not work even if I tried to define it the same way as the first relation between node--currency: when I create a new unit instance and add it to DbSet<CashFlowSpendableMinimumUnit> it does not appear in the navigation property currMinimum.CashFlowSpendableMinimumUnits and if I continue execution, an attempt to make cascade persistent by calling SaveChanges() later results in the error in the %subject%.
UPDATE 1
I omit exact wording of error message, here it is:
Unable to determine the principal end of the 'xxx.yyy.Database.CashFlowSpendableMinimumNode_CashFlowSpendableMinimumCurrencies' relationship. Multiple added entities may have the same primary key.
UPDATE 2
The only solution to make it running I've discovered is to flush the change tracker prematurely:
foreach (var node in nodes)
{
nodeMinimum = new ...
DbSet<CashFlowSpendableMinimumNode>.Add(nodeMinimum);
foreach (var curr in currencies)
{
currMinimum = new ...
DbSet<CashFlowSpendableMinimumCurrency>.Add(currMinimum);
DbContext.SaveChanges(); //The premature flush to fix a problem
foreach (var unit in units)
{
unitMinimum = new ...
DbSet<CashFlowSpendableMinimumUnit>.Add(unitMinimum);
}
}
}
DbContext.SaveChanges();
...
UPDATE 3
Concerning discused constructors and instance creation:
public CashFlowSpendableMinimumCurrency()
{
CashFlowSpendableMinimumUnits = new List<CashFlowSpendableMinimumUnit>();
}
[...]
CashFlowSpendableMinimumCurrency result = new CashFlowSpendableMinimumCurrency()
{
CurrencyCode = ACurrencyCode, // the attached entity
Curr = ACurrencyCode.Curr, // string
Minimum = minimum, //decimal
Median = median, //decimal
CashFlowSpendableMinimumNode = ANodeMinimum, // parent in 1:n relation, EntityState.Added
ExchangeRate = 1, //decimal
DeclaredExchangeRate = false,
BanknotesContribution = 0, //double
BanknotesSpendableMinimum = 0, //decimal
CoinsContribution = 0, //double
CoinsSpendableMinimum = 0, //decimal
};
ADbContext.CashFlowSpendableMinimumCurrencies.Add(result);
Please, does anybody see, what is wrong?
Thanks, pf