简体   繁体   English

DbContext 无法保存多个条目

[英]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 循环获取存储在数据库中的对象列表,这是我当前的代码:

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.由于某种原因,这实际上只存储了列表中的最后一个 object,我在循环之外有 SaveChanges(),因为我认为这会提高性能。

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:看到 ChangeTracker 显示每个条目的更改,我尝试将 SaveChanges() 放入循环中,但随后第一个条目被存储,第二个错误输出,因为它尝试使用与刚刚保存的条目相同的 ID:

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.您一遍又一遍地添加相同的Rule object。

When you add something to EF, it keeps track of that object.当您向 EF 添加内容时,它会跟踪 object。 This is how EF knows when an entity has been updated.这就是 EF 知道实体何时更新的方式。 EF is incapable of tracking the same in-memory object several times and pretending like they're different. EF 无法多次跟踪相同的内存 object 并假装它们不同。

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.第二次,EF 意识到这与之前的 object 相同,因此没有添加任何新内容 - 它已经在跟踪这个 object。

Make sure you add new objects, eg:确保添加对象,例如:

var currentRule = new Rule();

// set some values

_context.Rules.Add(currentRule);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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