簡體   English   中英

調用的目標已拋出實體框架異常

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

我正在嘗試制作一個非常簡單的實體框架代碼優先項目,但無法解決“調用目標已拋出異常”的問題。 當我試圖從我的數據庫中檢索數據時。

我有一個獨特的課程:

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."}

為什么我會收到這樣的錯誤?

實際錯誤顯示在內部異常中:

類“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

就我而言,我必須從 C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL 中刪除 EntityFramework 和 EntityFramework.SqlServer 文件夾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM