我在使用EF扩展BulkInsert(MVC5,EF6)时遇到了奇怪的问题。
这是我的代码结构: 我的db上下文继承自IdentityDbContext(我使用Owin) 实体界面
public interface IMyObject
{
Guid Id { get; set; }
}
基础实体对象
public class MyObject: IMyObject
{
[Key]
public Guid Id
{
get
{
if (_id == Guid.Empty)
{
_id = Guid.NewGuid();
}
return _id;
}
set
{
_id = value;
}
}
private Guid _id;
继承的实体对象
public class Client : MyObject
{
[Required]
[StringLength(255)]
public string Name { get; set; }
public string Town{get;set;}
....
}
我正在尝试使用BulkInsert将大量Client对象插入数据库:
private void InsertDataToDatabase<T>(ICollection<T> dbObjects) where T : class, IMyObject
{
MyContext.BulkInsert<T>(dbObjects, 5000);
//fails
}
然而,这种标准插入方法工作正常:
private void InsertDataToDatabase<T>(ICollection<T> dbObjects) where T : class, IMyObject
{
foreach (var entity in dbObjects)
{
MyContext.Set<T>().Add(entity);
}
MyContext.SaveChanges();
//works fine
}
BulkInsert中的exeption是:
System.Reflection.AmbiguousMatchException:找到了模糊匹配。在 System.RuntimeType.GetPropertyImpl(String name,BindingFlags bindingAttr,Binder binder,Type returnType,Type []类型, System.Type.GetProperty的ParameterModifier []修饰符)(String name, BindingFlags bindingAttr)at EntityFramework.MappingAPI.Extensions.TypeExtensions.GetProperty(类型 in,string,propertyName,Char separator) C:\ dev的\ EntityFramework.MappingAPI \树干\ SRC \ EntityFramework.MappingAPI \扩展\ TypeExtensions.cs:线 79点 EntityFramework.MappingAPI.Mappers.MapperBase.MapProperty(EntityMap entityMap,EdmProperty edmProperty,Int32&amp;我,弦乐&amp;前缀) C:\ dev的\ EntityFramework.MappingAPI \树干\ SRC \ EntityFramework.MappingAPI \映射器\ MapperBase.cs:线 488 at EntityFramework.MappingAPI.Mappers.MapperBase.MapEntity(String typeFullName,EdmType edmItem)in C:\ dev的\ EntityFramework.MappingAPI \树干\ SRC \ EntityFramework.MappingAPI \映射器\ MapperBase.cs:线 357 at EntityFramework.MappingAPI.Mappings.DbMapping..ctor(DbContext 上下文) C:\ dev的\ EntityFramework.MappingAPI \树干\ SRC \ EntityFramework.MappingAPI \映象\ DbMapping.cs:线 82在EntityFramework.MappingAPI.EfMap.Get(DbContext context)中 C:\ dev的\ EntityFramework.MappingAPI \树干\ SRC \ EntityFramework.MappingAPI \ EfMap.cs:线 60点到 EntityFramework.MappingAPI.Extensions.MappingApiExtensions.Db(的DbContext ctx,类型类型)in C:\ dev的\ EntityFramework.MappingAPI \树干\ SRC \ EntityFramework.MappingAPI \扩展\ MappingApiExtensions.cs:线 51点 System.Linq.Enumerable.ToDictionary [TSource,TKey,TElement](IEnumerable
1 source, Func
2 keySelector,Func2 elementSelector, IEqualityComparer
1 comparer)at at EntityFramework.BulkInsert.Helpers.MappedDataReader1..ctor(IEnumerable
1 enumerable,IEfBulkInsertProvider provider)in C:\ dev的\ EntityFramework.BulkInsert \ dev的\ SRC \ EntityFramework.BulkInsert \助手\ MappedDataReader.cs:线 58点 EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run [T](IEnumerable1 entities, SqlTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\EfSqlBulkInsertProviderWithMappedDataReader.cs:line 22 at EntityFramework.BulkInsert.Providers.ProviderBase
2.Run [T](IEnumerable1 entities, IDbTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 77 at EntityFramework.BulkInsert.Providers.ProviderBase
2.Run [T](IEnumerable1 entities, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 105 at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable
1个实体,SqlBulkCopyOptions sqlBulkCopyOptions,Nullable1 batchSize) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:line 95 at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable
1个实体,Nullable1 batchSize) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:line 75 at My.Portal.Controllers.InstallController.InsertDataToDatabase[T](ICollection
1 对象) C:\ XXXX \ \控制器InstallController.cs:行 581
请帮忙。