我有两张桌子INS_Staging_UMLERTransaction(Parent)和INS_Staging_UMLERBlueCard(Child)。
我需要插入1000条记录,当我使用bulk insert时,它只插入父表。以下是我的代码。
_indnCon.BulkInsert(_DataToTrans);
_indnCon.BulkInsert(_DataToTrans.SelectMany(m =>
m.INS_Staging_UMLERBlueCard));
_indnCon.BulkSaveChanges();发布于 2017-07-04 20:49:59
实体框架扩展中没有BulkInsert或BulkSaveChanges方法。
因此,在我的回答中,我将假设您使用的是Entity Framework Extensions库。
从v3.12.6 (Release Note)开始,添加了IncludeGraph选项以包含子实体。
示例
_indnCon.BulkInsert(_DataToTrans, operation => operation.IncludeGraph = true); 注意:在调用BulkInsert之后,您不必调用BulkSaveChanges。批量操作直接在数据库中执行。
答案子问题:
当我使用IncludeGraph时,
我得到一个异常。例外情况是“必须存在默认的DbContext上下文,或者必须提供上下文工厂(EntityFrameworkManager.ContextFactory)。IncludeGraph等某些功能需要此设置。”
这个错误意味着你的上下文没有默认的构造函数。此功能需要一种构建新上下文的方法。
下面是一个在不存在默认构造函数的情况下如何指定上下文工厂的示例:
EntityFrameworkManager.ContextFactory = context => new CurrentContext(My.ConnectionString);发布于 2021-06-13 15:54:28
这就是我解决问题的方法。已使用Postgres数据库。
services.AddDbContextPool<postgresContext>(options =>
{
options.UseNpgsql(Configuration.GetConnectionString("PostgresConString"));
});//在出现此类错误时添加此行
EntityFrameworkManager.ContextFactory = context =>
{
var optionsBuilder = new DbContextOptionsBuilder<postgresContext>();
optionsBuilder.UseNpgsql(Configuration.GetConnectionString
("PostgresConString"));
return new postgresContext(optionsBuilder.Options);
};https://stackoverflow.com/questions/44898323
复制相似问题