[英]DbContext failing to save multiple entries
我正在尝试通过 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();
由于某种原因,这实际上只存储了列表中的最后一个 object,我在循环之外有 SaveChanges(),因为我认为这会提高性能。
当我运行它时,我得到以下信息:
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'
看到 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'
我知道我一定做错了什么,但我找不到什么!
您一遍又一遍地添加相同的规则。 尝试类似的东西
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);
您一遍又一遍地添加相同的Rule
object。
当您向 EF 添加内容时,它会跟踪 object。 这就是 EF 知道实体何时更新的方式。 EF 无法多次跟踪相同的内存 object 并假装它们不同。
第一次,您的实体被添加。
第二次,EF 意识到这与之前的 object 相同,因此没有添加任何新内容 - 它已经在跟踪这个 object。
确保添加新对象,例如:
var currentRule = new Rule();
// set some values
_context.Rules.Add(currentRule);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.