Error using Entity Framework Bulkinsert

Asked

Viewed 288 times

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 created automatically 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 fill my list of products, I did it as follows:

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 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)}
  • What You Do With Your Products List = Products ?

  • In the first block of question code, I use the products variable for Bulkinsert.

1 answer

3


It turns out that Bulkinsert only works with Databasefirst if you are using Entityframework 6.

You can confirm this at package Codeplex page.

At Stackoverflow, there’s a question [from two years ago] similar to yours, where the developer of the package says it was originally meant to be used with Codefirst.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.