【问题标题】:Entity Framework Exception has been thrown by the target of an invocation实体框架异常已被调用的目标抛出
【发布时间】:2016-10-08 15:42:49
【问题描述】:

我正在尝试制作一个非常简单的实体 Framework Code First 项目,但无法解决“调用目标已引发异常”。当我试图从我的数据库中检索数据时。

我有一个独特的课程:

public class TestStylo
{
    [Key]
    public int TestStyloID { get; set; }
    StyloType styloType { get; set; }
    public string name;

    public TestStylo(StyloType styloType, string name)
    {
        this.styloType = styloType;
        this.name = name;
    }
    public StyloType getTypeStylo{
        get { return styloType; }
    }


}
public enum StyloType
{
    pen,
    wood,
    gold
}

我的模特是:

public class Model1 : DbContext
{
     public Model1()
        : base("name=Model1")
    {
    }

    public  DbSet<TestStylo> MyStylos { get; set; }
}

而我的 App.Config 是:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="Model1" connectionString="data source=(LocalDb)\v11.0;initial catalog=TestCodeFirst.Model1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

我的 connectionString 正在工作,我可以手动打开并查看我的数据库及其内容。 当我尝试向我的数据库添加内容时,我使用:

using (Model1 db = new Model1())
            {
                TestStylo stylo = new TestStylo(StyloType.pen,"numberOne");
                db.MyStylos.Add(stylo);
                db.SaveChanges();
            }

它在大多数情况下都有效(它只插入 ID .. 但我稍后会看到)

但是当我尝试检索时:

private void button2_Click(object sender, EventArgs e)
        {
            using (Model1 db = new Model1())
            {
                var stylos = from order in db.MyStylos
                              select order;

                ...
            }
        }

我收到一个带有内部异常 {"The class 'TestCodeFirst.TestStylo' has no parameterless constructor."}"Exception has been thrown by the target of an invocation."

为什么会出现这样的错误?

【问题讨论】:

    标签: c# entity-framework code-first


    【解决方案1】:

    实际的错误出现在内部异常中:

    “TestCodeFirst.TestStylo”类没有无参数构造函数。

    所有实体都应该有一个无参数的构造函数,以便实体框架使用。

    您可以简单地将这个构造函数添加到您的实体中:

    public class TestStylo
    {
        [Key]
        public int TestStyloID { get; set; }
        StyloType styloType { get; set; }
        public string name;
    
        public TestStylo(StyloType styloType, string name)
        {
            this.styloType = styloType;
            this.name = name;
        }
        public StyloType getTypeStylo{
            get { return styloType; }
        }
    
        // Add this:
        public TestStylo()
        {
    
        }
    }
    

    如果您不想公开公共构造函数,这个构造函数实际上可以是internalprivate

    【讨论】:

    • 它确实修复了我的错误。但似乎它在没有任何参数但 TestStyloID 的情况下插入数据库,我怎样才能确保我也将 StyloType 和名称保存在数据库中?
    • @Belte 当您想要创建要插入的实体时,您可以继续使用TestStylo(StyloType styloType, string name) 构造函数。 Entity Framework 将使用无参数构造函数,f.i.从数据库读取时。
    • 我是,我只添加了你告诉我的空构造函数,并尝试为我的 name 字段添加 get 和 setter。调试时,我可以看到我正在插入一个带有名称和类型的TestStylo,但是在检索我的数据时,我奇怪地只有一个 ID、一个类型,但名称为 null。
    【解决方案2】:

    在我的情况下,我必须从 C:\Windows\Microsoft.NET\assembly\GAC_MSIL 中删除 EntityFramework 和 EntityFramework.SqlServer 文件夹

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      相关资源
      最近更新 更多