Entity Framework 调用目标抛出异常

Entity Framework Exception has been thrown by the target of an invocation

我正在尝试制作一个非常简单的 entity Framework Code First 项目,但是当我尝试从我的数据库中检索数据时无法解决 "Exception has been thrown by the target of an invocation."。

我有一个独一无二的 class :

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;

                ...
            }
        }

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

为什么我会收到这样的错误?

真正的错误出现在内部异常中:

The class 'TestCodeFirst.TestStylo' has no parameterless constructor.

所有实体都应具有无参数构造函数,以便 Entity Framework 使用。

您只需将此构造函数添加到您的实体即可:

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()
    {

    }
}

如果您不想公开 public 构造函数,此构造函数实际上可以是 internalprivate

在我的例子中,我不得不从 C:\Windows\Microsoft.NET\assembly\GAC_MSIL

中删除 EntityFramework 和 EntityFramework.SqlServer 文件夹