【问题标题】:Could not load type 'XXX' from assembly 'XXX'. at.OnModelCreating(DbModelBuilder modelBuilder)无法从程序集“XXX”加载类型“XXX”。 at.OnModelCreating(DbModelBuilder modelBuilder)
【发布时间】:2016-07-29 13:25:50
【问题描述】:

建立模型后,我想对其进行审查,因此我尝试使用 EntityFramework Power 工具直观地获取模型,但出现以下错误:

System.Reflection.TargetInvocationException:已抛出异常 通过调用的目标。 ---> System.TypeLoadException:可以 不从程序集中加载类型“DomainClasses.CompanyLogo” '域类,版本=1.0.0.0,文化=中性, PublicKeyToken=null'。在 DataLayer.FinanceModelContext.OnModelCreating(DbModelBuilder 模型构建器)在 System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext 内部上下文)在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput 输入)在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
在 System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized() 在 System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext


可能导致错误的我的域类:

namespace DomainClasses
{
    public class CompanyLogo
    {
        public byte[] Logo { get; set; }

        public int CompanyId { get; set; }

        public virtual Company Company { get; set; }
    }
}

配置文件:

public class CompanyLogoMappings:EntityTypeConfiguration<CompanyLogo>
    {
        public CompanyLogoMappings()
        {
            this.HasKey(c => c.CompanyId);
            this.HasRequired(c => c.Company).WithOptional(cl => cl.CompanyLogo);
        }
    }

【问题讨论】:

  • 您是否已将 CompanyLogoMappings 添加到配置中?
  • @AdilMammadov : 是的modelBuilder.Configurations.Add(new CompanyLogoMappings()); 但我不知道为什么会出现这个错误

标签: c# entity-framework visual-studio-2015 ef-code-first entity-framework-6


【解决方案1】:

经过多次跟踪,我找到了出现此错误的原因。

通过EF power tools生成模型后,我添加了域类CompanyLogo

所以我重新启动 VS 并构建域类项目并运行该工具,所以它可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2019-12-12
    • 2013-07-18
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多