【问题标题】:VS 2017 No parameterless constructor defined for this objectVS 2017 没有为此对象定义无参数构造函数
【发布时间】:2017-09-04 07:31:44
【问题描述】:

我在 VS 2017 中基于 .net core 2 创建了一个项目。当我想使用实体框架创建一个新控制器时,我收到一条错误消息:

运行所选代码生成器时出错: 没有为此对象定义无参数构造函数

这是我的模型:

public class Country : Entity<int>
{
    public Country()
    {

    }

    [Required]
    [MaxLength(100)]
    [Display(Name = "Country Name")]
    public string Name { get; set; }

    public virtual IEnumerable<Person> Persons { get; set; }
}

我应该如何解决这个问题?

更新: 这是我的Entity 课程:

public abstract class BaseEntity { 

}

public abstract class Entity<T> : BaseEntity, IEntity<T> 
{
    public virtual T Id { get; set; }
}

【问题讨论】:

  • 什么是Entity,它有默认(无参数)构造函数吗?
  • 我已经更新了我的问题,请再次查看。
  • 在BaseEntity和Entity中放一个默认的无参构造函数
  • @Mardoxx 它没有任何效果,我仍然得到同样的错误。
  • 你是如何创建控制器的?

标签: asp.net-mvc asp.net-core


【解决方案1】:

我刚遇到同样的问题,我意识到它与无参数构造函数无关,您缺少 Id 属性,只需添加

public string Id {get; set;}

到模型,你应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2016-04-25
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多