实体框架-实体查找不抛出无参数构造函数
本文关键字:实体 参数 构造函数 框架 查找 | 更新日期: 2023-09-27 18:14:48
我让"Find"方法在执行时抛出这个异常:异常已被调用的目标抛出。——>系统。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;
}
所有实体必须有一个无参数构造函数。如果需要,它可以是私有的:
http://social.technet.microsoft.com/wiki/contents/articles/3820.entity-framework-faq-entity-classes.aspx Does_the_Entity_Framework_require_objects_with_public_empty_constructors
你必须给NWatchConvertMethod添加一个无参数的构造函数