简体   繁体   English

反向poco c#批量插入实体框架“对象引用未设置为对象的实例”

[英]Reverse poco c# bulk insert Entity Framework “object reference not set to an instance of an object”

Weird situation: I'm getting a bulkinsert failure using the code below. 奇怪的情况:使用以下代码,我发生了批量插入失败。 The weird thing is it works if I set the DbContext name to a specific name, but fails if I change it. 奇怪的是,如果我将DbContext名称设置为特定名称,它将起作用,但是如果更改它,它将失败。

With nothing else different, if I edit the reverse poco tt file, and change the DbContext name to a specific name and save it (recreating the Entity Framework files), it works. 没什么不同,如果我编辑反向poco tt文件,并将DbContext名称更改为特定名称并保存(重新创建Entity Framework文件),则可以使用。 If I do the same thing but change it in any way, it will fail when the code hits the bulkinsert() line. 如果我做同样的事情但以任何方式进行更改,则当代码到达bulkinsert()行时,它将失败。

It makes absolutely no sense to me. 这对我来说绝对没有意义。 I can't set it to the working one because that working one is already being used in the same project (I tested this in a new project just trying to figure out the problem.) 我无法将其设置为工作中的一个,因为该工作中的一个已在同一项目中使用(我在一个新项目中对此进行了测试,试图找出问题所在。)

using (var transactionScope = new TransactionScope())
{
    db.BulkInsert(recordsToInsert);
    db.SaveChanges();
    transactionScope.Complete();
}

Any ideas? 有任何想法吗? I know it's not something to do with the table or the fields, because it works with one dbcontextname and not any other. 我知道这与表或字段dbcontextname ,因为它可以与一个dbcontextname而不能与其他任何一个dbcontextname

Edit: also, the exception stack trace shows that it's failing here: 编辑:此外,异常堆栈跟踪显示它在这里失败:

at EntityFramework.BulkInsert.ProviderFactory.Get(DbContext context)

Fakhar Ahmad Rasul posted the solution in the comments. Fakhar Ahmad Rasul在评论中发布了解决方案。 It turns out that the dbcontext name has to be the same as the connection string name. 事实证明,dbcontext名称必须与连接字符串名称相同。 Thanks Fakhar! 谢谢Fakhar! I wish I could award you points or something. 希望我能给您积分或其他奖励。

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

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