Entity Framework - 实体查找不抛出无参数构造函数

Entity Framework - Entity Find throwing no parameterless constructor

我有 "Find" 方法在执行时抛出这个异常: 调用的目标抛出了异常。 ---> System.InvalidOperationException: class '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 添加无参数构造函数。