简体   繁体   English

实体框架-实体查找不抛出无参数构造函数

[英]Entity Framework - Entity Find throwing no parameterless constructor

I have the "Find" method throwing this exception when executed: Exception has been thrown by the target of an invocation. 执行时,我有“查找”方法引发此异常:调用的目标已引发异常。 ---> System.InvalidOperationException: The class 'NWatch.Entities.NWatchConvertMethod' has no parameterless constructor. ---> System.InvalidOperationException:类'NWatch.Entities.NWatchConvertMethod'没有无参数的构造函数。

Adding the ConvertMethod to the dbContext and saving changes has no problems. 将ConvertMethod添加到dbContext并保存更改没有问题。

Code: 码:

[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;
        }

All entities must have a parameterless constructor. 所有实体必须具有无参数的构造函数。 It can be private if you want: 如果需要,它可以是私有的:

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

You must add a parameterless constructor to NWatchConvertMethod. 您必须向NWatchConvertMethod添加无参数构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM