[英]Entity Framework - Entity Find throwing no parameterless constructor
執行時,我有“查找”方法引發此異常:調用的目標已引發異常。 ---> System.InvalidOperationException:類'NWatch.Entities.NWatchConvertMethod'沒有無參數的構造函數。
將ConvertMethod添加到dbContext並保存更改沒有問題。
碼:
[TestMethod]
public void Delete()
{
// Add the entry
var convertMethod = RenderConvertMethod();
dbContext.ConvertMethods.Add(convertMethod);
dbContext.SaveChanges();
int id = convertMethod.ConvertMethodId;
// Remove entry
dbContext.ConvertMethods.Remove(convertMethod);
dbContext.SaveChanges();
// Ensure the entry no longer exists in the DB
// BELOW LINES THROWS THE EXCEPTION
var convertMethodFromDb = dbContext.ConvertMethods.Find(id);
Assert.IsNull(convertMethodFromDb);
}
private NWatchConvertMethod RenderConvertMethod()
{
var convertMethod = new NWatchConvertMethod("TestConvertMethod")
{
ConvertMethodDesc = "my description"
};
return convertMethod;
}
所有實體必須具有無參數的構造函數。 如果需要,它可以是私有的:
您必須向NWatchConvertMethod添加無參數構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.