简体   繁体   English

ArgumentNullException:值不能为null。参数名称:构造函数

[英]ArgumentNullException: Value cannot be null. Parameter name: constructor

I'm building a ASP.NET 5 MVC application, using EF 7. I've got three models: The Book class: 我正在使用EF 7构建一个ASP.NET 5 MVC应用程序。我有三个模型:The Book类:

public class Book : IBook<MangaChapter>, IHasThumbnail, IBugChecker
    {
        public Book()
        {
            Chapters = new List<MangaChapter>();
        }

        [Key]
        public int ID { get; set; }


        [Required]
        public string Title { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        public string ThumbnailPath { get; set; }

        public virtual IList<Chapter> Chapters { get; set; }
}

The chapter class: 章节类:

public class MangaChapter : IChapter<MangaBook>, IHasThumbnail
    {
        public MangaChapter()
        {
            PagesPath = new List<Path>();
        }

        [Key]
        public int ID { get; set; }

        [Required]
        public string Title { get; set; }

        public string ThumbnailPath { get; set; }

        public IList<Path> PagesPath { get; set; }

        public int BookID { get; set; }
        public MangaBook Book { get; set; }
    }
}

and the path class, that I introduced just to store a list in EF 和路径类,我介绍的只是为了在EF中存储一个列表

public class Path
    {
        public Path(string s) { P = s; }

        public int ID { get; set; }

        public string P { get; set; }

        public int MangaChapterID { get; set; }

        public MangaChapter MangaChapter { get; set; }

        public static implicit operator Path(string s)
        {
            return new Path(s);
        }

        public static implicit operator string(Path s)
        { return s.P; }
    }

the Problem is when I try to access the database using myDbContext, in this way: 问题是当我尝试使用myDbContext访问数据库时,这样:

var list = _db.Paths.ToList();

I get a Exception: ArgumentNullException: Value cannot be null. 我得到一个异常:ArgumentNullException:值不能为null。 Parameter name: constructor. 参数名称:构造函数。 I've tried to access it in different ways, but always ended up with that exception. 我试图以不同的方式访问它,但总是以该异常结束。

PS the source code for the application is entirely on GitHub PS应用程序的源代码完全在GitHub上

You need a default constructor on the Path object: 您需要Path对象上的默认构造函数:

public Path()
{
}

Entity Framework (much like all the JSON Serializers, XML Serializers, and other serailizers I can think of) requires a default constructor on all objects in order to properly serialize/save and deserailize/load it. 实体框架(很像我所能想到的所有JSON Serializers,XML Serializers和其他serailizers)需要在所有对象上使用默认构造函数,以便正确地序列化/保存和取消选择/加载它。

This question answers it pretty well: 这个问题很好地解答了:

No parameterless constructor defined for type of 'System.String' during JSON deserialization 在JSON反序列化期间,没有为'System.String'类型定义无参数构造函数

Basically, the Serailizer/Deserailizer needs to create an empty object to assign values to it. 基本上,Serailizer / Deserailizer需要创建一个空对象来为其赋值。 It is not capable of making a complex decision of deciding what constructor to use. 它无法做出决定使用什么构造函数的复杂决策。 Yes, an argument could be made that it could infer what properties go into the constructor by the names, but in your situation it cannot even do that (which it wouldn't even try to, but that's beyond my point). 是的,可以说它可以推断出哪些属性通过名称进入构造函数,但在你的情况下它甚至不能这样做(它甚至不会尝试,但这超出了我的观点)。 The parameter in your constructor would is called s , but you have no property with which to match up s . 在构造函数中的参数就会被称为s ,但你有没有财产,用以匹配s (Again, this is not anything it would try, but if it could it would fail.) (同样,这不是它会尝试的任何东西,但如果它可能会失败。)

Basically, if you plan on doing any serialization/deserialization/database saving/database loading of the type, you need to implement a default constructor, even if all it does is create a completely emptied object. 基本上,如果您计划对类型进行任何序列化/反序列化/数据库保存/数据库加载,则需要实现默认构造函数,即使它所做的只是创建一个完全清空的对象。

暂无
暂无

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

相关问题 System.ArgumentNullException:&#39;值不能为null。 参数名称:键 - System.ArgumentNullException: 'Value cannot be null. Parameter name: key' ArgumentNullException:值不能为空。 参数名称:connectionString - ArgumentNullException: Value cannot be null. Parameter name: connectionString ArgumentNullException:值不能为null。 参数名称:实体 - ArgumentNullException: Value cannot be null. Parameter name: entity System.ArgumentNullException: &#39;值不能为空。 参数名称:items&#39; - System.ArgumentNullException: 'Value cannot be null. Parameter name: items' System.ArgumentNullException: '值不能是 null。 参数名称:providerInvariantName' - System.ArgumentNullException: 'Value cannot be null. Parameter name: providerInvariantName' ArgumentNullException:值不能为null。 参数名称:包含语句上的键 - ArgumentNullException: Value cannot be null. Parameter name: key on Include Statement System.ArgumentNullException: &#39;值不能为空。 参数名称:提供者&#39; - System.ArgumentNullException: 'Value cannot be null. Parameter name: provider' System.ArgumentNullException:“值不能是 null。 参数名称:实体” - System.ArgumentNullException: “Value cannot be null. Parameter name: entity” ArgumentNullException:值不能为 null。 (参数“项目”) - ArgumentNullException: Value cannot be null. (Parameter 'items') ArgumentNullException:值不能为 null。 (参数“用户”) - ArgumentNullException: Value cannot be null. (Parameter 'user')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM