简体   繁体   中英

DbContext failing to save multiple entries

I am trying to get a list of objects stored in the database through a foreach loop, here is my current code:

foreach (var i in ruleset)
{
    var currentRule = Rule;
    currentRule.OriginIP = i[0];
    currentRule.DestinationIP = i[1];
    currentRule.Protocol = (Protocol)Enum.Parse(typeof(Models.Protocol), i[2]);
    currentRule.Ports = i[3];
    _context.Rules.Add(currentRule);
    Console.WriteLine(_context.ChangeTracker.DebugView.LongView);
    Console.WriteLine(currentRule.RuleID);

}
_context.SaveChanges();

For some reason this only actually stores the last object in the list, I have SaveChanges() outside of the loop as I assumed that would increase performance.

When I run this I get the following:

rule {RuleID: -2147482647} Added
  RuleID: -2147482647 PK Temporary
  CreationDate: '26/01/2021 14:16:10'
  DestinationIP: '10.232.20.20'
  Enabled: 'False'
  OriginIP: '192.168.10.10'
  Ports: '80, 443'
  Protocol: 'TCP'

0

rule {RuleID: -2147482647} Added
  RuleID: -2147482647 PK Temporary
  CreationDate: '26/01/2021 14:16:10'
  DestinationIP: '10.232.20.21' Originally '10.232.20.20'
  Enabled: 'False'
  OriginIP: '192.168.10.11' Originally '192.168.10.10'
  Ports: '80, 444' Originally '80, 443'
  Protocol: 'TCP'

Seeing the ChangeTracker show a change for each entry, I tried to put SaveChanges() inside the loop, but then the first entry is stored and the second errors out since it attempts to use the same ID as the entry it has just saved:

rule {RuleID: -2147482647} Added
  RuleID: -2147482647 PK Temporary
  CreationDate: '26/01/2021 14:25:40'
  DestinationIP: '10.232.20.20'
  Enabled: 'False'
  OriginIP: '192.168.10.10'
  Ports: '80, 443'
  Protocol: 'TCP'

62

rule {RuleID: 62} Added
  RuleID: 62 PK
  CreationDate: '26/01/2021 14:25:40'
  DestinationIP: '10.232.20.21' Originally '10.232.20.20'
  Enabled: 'False'
  OriginIP: '192.168.10.11' Originally '192.168.10.10'
  Ports: '80, 444' Originally '80, 443'
  Protocol: 'TCP'

I know I must be doing something wrong but I can't find what!

You're adding the same Rule over and over. Try something like

foreach (var i in ruleset)
{
    var currentRule = new Rule();
    currentRule.OriginIP = i[0];
    currentRule.DestinationIP = i[1];
    currentRule.Protocol = (Protocol)Enum.Parse(typeof(Models.Protocol), i[2]);
    currentRule.Ports = i[3];
    _context.Rules.Add(currentRule);
}
_context.SaveChanges();
var currentRule = Rule;

_context.Rules.Add(currentRule);

You keep adding this same Rule object over and over.

When you add something to EF, it keeps track of that object. This is how EF knows when an entity has been updated. EF is incapable of tracking the same in-memory object several times and pretending like they're different.

The first time, your entity gets added.
The second time, EF realizes that this is the same object as before, and therefore does not add anything new - it was already tracking this object anyway.

Make sure you add new objects, eg:

var currentRule = new Rule();

// set some values

_context.Rules.Add(currentRule);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM