簡體   English   中英

實體框架-實體查找不拋出無參數構造函數

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

所有實體必須具有無參數的構造函數。 如果需要,它可以是私有的:

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添加無參數構造函數。

暫無
暫無

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

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