2017-08-30 11 views
0

私は以下のようにBulkInsertメソッドを作成しました:私はCTX変数を展開したときに、私が見つけた、デバッグモードでEntity FrameworkのBulkInsert

Type 'Infrastructure.EF.tblUser' is not found in context 'Infrastructure.EF.Entities'

public void BulkInsertUsers(IEnumerable<tblUsers> users) 
{ 
    using (var ctx = new Entities()) 
    { 
     ctx.BulkInsert(users); 
     ctx.SaveChanges(); 
    } 
} 

ムーの問題は、ctx.BulkInsert(users);返すエラーですいくつかのエンティティは次のような値を持っています:

System.Data.Entity.DbSet<Automation.Infrastructure.EF.EntityName> 

その他:

SELECT 
    [Extent1].[ID] AS [ID], 
    [Extent1].[Name] AS [Name], 
FROM [dbo].[tbl] AS [Extent1] 

SELECT値を持つエンティティの場合、BulkInsertは正常に動作します。 私はあなたがEntitiesコンテキスト内のエンティティUsersを追加しようとしている

EntityFramework.BulkInsert, Version=6.0.2.8

+1

コンテキストでエンティティに 'users'を追加する必要があります。 –

+0

@DanielShillcockそれはどういう意味ですか? – ironcurtain

+0

あなたのコンテキスト( 'ctx')はエンティティ' Entities'を期待しており、あなたはそれに 'tblUser'を追加しています。 –

答えて

2

を使用しています。あなたが別のエンティティをBulkInsertするための一般的な方法を作ることができる...

public void BulkInsertUsers(IEnumerable<tblUsers> users) 
{ 
    using (var ctx = new tblUsers()) 
    { 
     // users is detached from database, so we need to add it 
     ctx.Attach(users); 
     // Specify that these are modified entities (may not be necessary for BulkInsert) 
     // I don't know what properties users contain, so used Name 
     ctx.Entry(tblUsers).Property(a => a.Name).IsModified = true; 
     ctx.BulkInsert(users); 
     ctx.SaveChanges(); 
    } 
} 

の下に考える何かを参照してください。これはあなたを助けるかもしれません:A generic way to save an entity in Entity Framework