Error while using Entity Framework BulkInsert

6

I have an Asp.Net MVC project in .Net Framework 4.0 with Entity Framework 5.0, where I added the BulkInsert- ef5 , but in the following excerpt error occurs:

using (MeuEntities context = new MeuEntities())
{
    context.Configuration.AutoDetectChangesEnabled = false; //  Com ou sem essa linha o resultado é o mesmo erro.
    using (TransactionScope scope = new TransactionScope())
    {
        context.BulkInsert(produtos); // Daqui vai direto pra Exception
    }
}

My class Produto was automatically created by EF:

namespace meuProjeto.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Produto
    {
        public int ProdutoId { get; set; } // Auto incremento no Banco de Dados (SQL Server)
        public string Descricao { get; set; }
        public Nullable<int> CategoriaId { get; set; }

        public virtual Categoria Categoria { get; set; }
    }
}

To complete my product list, I did the following:

ProdutoImport[] produtosImport = null;
var postedFile = Request.Files[0];
using (var reader = new StreamReader(postedFile.InputStream))
{
    var engine = new FileHelperEngine<ProdutoImport>();
    produtosImport = engine.ReadStream(reader);
}

var produtos = produtosImport.Select(p => new Produto
{
    Descricao = p.Descricao,
    Categoria = p.CategoriaId
})).ToList();

My class ProdutoImport :

[DelimitedRecord(";")]
public class ProdutoImport
{
    public string Descricao { get; set; }
    public int CategoriaId { get; set; }
}

All the rows in my file have both columns: Descrição;CategoriaId .

The error generated is:

  

CSSpace does not have an associated collection.

InnerException : Null

StackTrace:

   em System.Data.Metadata.Edm.MetadataWorkspace.GetItemCollection(DataSpace dataSpace, Boolean required)
   em System.Data.Metadata.Edm.MetadataWorkspace.GetItemCollection(DataSpace dataSpace)
   em EntityFramework.MappingAPI.Mappers.MapperBase.get_TphData() na c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\MapperBase.cs:linha 59
   em EntityFramework.MappingAPI.Mappers.MapperBase.MapEntity(String typeFullName, EdmType edmItem) na c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\MapperBase.cs:linha 284
   em EntityFramework.MappingAPI.Mappings.DbMapping..ctor(DbContext context) na c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappings\DbMapping.cs:linha 80
   em EntityFramework.MappingAPI.EfMap.Get(DbContext context) na c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\EfMap.cs:linha 60
   em EntityFramework.MappingAPI.Extensions.MappingApiExtensions.Db(DbContext ctx, Type type) na c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Extensions\MappingApiExtensions.cs:linha 51
   em System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable'1 source, Func'2 keySelector, Func'2 elementSelector, IEqualityComparer'1 comparer)
   em System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable'1 source, Func'2 keySelector, Func'2 elementSelector)
   em EntityFramework.BulkInsert.Helpers.MappedDataReader'1..ctor(IEnumerable'1 enumerable, IEfBulkInsertProvider provider) na c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Helpers\MappedDataReader.cs:linha 58
   em EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run[T](IEnumerable'1 entities, SqlTransaction transaction, BulkInsertOptions options) na c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\EfSqlBulkInsertProviderWithMappedDataReader.cs:linha 22
   em EntityFramework.BulkInsert.Providers.ProviderBase'2.Run[T](IEnumerable'1 entities, IDbTransaction transaction, BulkInsertOptions options) na c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:linha 77
   em EntityFramework.BulkInsert.Providers.ProviderBase'2.Run[T](IEnumerable'1 entities, BulkInsertOptions options) na c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:linha 105
   em EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable'1 entities, SqlBulkCopyOptions sqlBulkCopyOptions, Nullable'1 batchSize) na c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:linha 95
   em EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable'1 entities, Nullable'1 batchSize) na c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:linha 75
   em meuProjeto.Areas.Administrador.Controllers.MeuController.MinhaAction(Int32 param1, Int32 param2) na c:\...\meuProjeto\Areas\Administrador\Controllers\MeuController.cs:linha 8567

TargetSite

{System.Data.Metadata.Edm.ItemCollection GetItemCollection(System.Data.Metadata.Edm.DataSpace, Boolean)}
    
asked by anonymous 02.06.2016 / 15:47

1 answer

3

It turns out that BulkInsert only works with DatabaseFirst if you are using EntityFramework 6.

You can confirm this in the Codeplex page .

In StackOverflow, you have a question [from two years ago] similar to yours, where the package developer says that it was made at first to be used with CodeFirst.

    
02.06.2016 / 16:39