调用的目标已抛出实体框架异常

Bel*_*ius 3 c# entity-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
}
Run Code Online (Sandbox Code Playgroud)

我的模型是:

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

    public  DbSet<TestStylo> MyStylos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

但是当我尝试检索时:

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

                ...
            }
        }
Run Code Online (Sandbox Code Playgroud)

我收到了"Exception has been thrown by the target of an invocation."一个 Inner-Exception {"The class 'TestCodeFirst.TestStylo' has no parameterless constructor."}

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

Yel*_*yev 6

实际错误显示在内部异常中:

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

所有实体都应该有一个无参数的构造函数,以便 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()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

这个构造函数实际上可以是internal或者private如果您不想公开公共构造函数。