我尝试使用EntityFramework.BulkInsert
扩展程序,但我不断获得Type 'MyEntity.CallDetail' is not found in context 'MyEntity.MyEntities'
的例外情况。我将此例外跟踪到DBMapping
中的EntityFramework.MappingAPI
类。抛出它的方法是:
public IEntityMap this[string typeFullName]
{
get
{
if (!_tableMappings.ContainsKey(typeFullName))
throw new Exception("Type '" + typeFullName + "' is not found in context '" + _contextTypeName + "'");
return _tableMappings[typeFullName];
}
}
我的问题是,这是EntityFramework.MappingAPI
中的错误还是我的代码中的这个错误?
我的上下文类:
public partial class MyEntities : DbContext
{
public MyEntities()
: base("name=MyEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<CallDetail> CallDetail { get; set; }
public virtual DbSet<PhoneState> PhoneStates { get; set; }
public virtual DbSet<Skill> Skills { get; set; }
public virtual DbSet<Team> Teams { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<StateLog> StateLogs { get; set; }
}
我假设错误是由DBSet<...>
而不仅仅是类型引起的,但我可能错了。
如果它在我的代码中(最有可能),我需要做些什么才能使其工作?
编辑:感谢@BenRobinson,问题归结于EF映射。我发现其他人在找到here,here和here的数据库第一个模型时遇到与EntityFramework.MappingAPI
类似的问题。由于我是EF的新手,我希望有人可以在edmx
中看到可能导致问题的问题。以下是edmx文件中的信息:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="my_contactModel.Store" Provider="MySql.Data.MySqlClient" ProviderManifestToken="5.6" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityType Name="call_detail">
....
</EntityType>
<EntityContainer Name="my_contactModelStoreContainer">
<EntitySet Name="call_detail" EntityType="Self.call_detail" Schema="my_contact" store:Type="Tables" />
....
</EntityContainer>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyContactModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="CallDetail">
....
</EntityType>
<EntityContainer Name="MyEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="CallDetail" EntityType="MyContactModel.CallDetail" />
....
</EntityContainer>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="my_contactModelStoreContainer" CdmEntityContainer="MyEntities">
<EntitySetMapping Name="CallDetail">
<EntityTypeMapping TypeName="MyContactModel.CallDetail">
<MappingFragment StoreEntitySet="call_detail">
....
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
</edmx:Edmx>
答案 0 :(得分:11)
我遇到了同样的问题,只需更新我的EntityFramework.MappingAPI即可解决问题! NuGet MappingAPI。更新后,我只是重建了我的项目,不再有例外。
答案 1 :(得分:6)
这个确切的错误和建议的解决方案对我有用。安装EntityFramework.BulkInsert-ef6软件包后,返回Manage NuGet Packages,应该有一个EntityFramework.MappingAPI更新。更新后,运行context.BulkInsert(listOfObjects);它应该成功。
答案 2 :(得分:1)
EntityFramework.MappingAPI和EntityFramework.BulkInsert-EFx不是偶像。应该使用Z.EntityFramework.Extensions包。
https://www.nuget.org/packages/Z.entityframework.extensions
现在语法略有不同(例如db.BulkInsert(addresses, bulk => bulk.InsertKeepIdentity = true);
),但对我来说效果很好。