简体   繁体   中英

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.

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. If I do the same thing but change it in any way, it will fail when the code hits the bulkinsert() line.

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.

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. It turns out that the dbcontext name has to be the same as the connection string name. Thanks Fakhar! I wish I could award you points or something.

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