简体   繁体   English

不包含带有2个参数的构造函数

[英]Does not contain a constructor that takes 2 arguments

I'm not sure what is occurring here. 我不确定这里发生了什么。 The model was auto generated from the database and I can't see anything obvious (mind you it is 2.30am UK time at the moment so maybe I'm half asleep). 该模型是从数据库自动生成的,我看不到任何明显的信息(请注意,此时此刻是英国时间凌晨2:30,所以我可能半睡着了)。 I am getting the error: ActiveCitizenSystemMimic.Models.ActiveCitizenProperties does not contain a constructor that takes 2 arguments. 我收到错误消息:ActiveCitizenSystemMimic.Models.ActiveCitizenProperties不包含带有2个参数的构造函数。

Model: 模型:

namespace ActiveCitizenSystemMimic.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ActiveCitizenProperties
    {
        public int FK_ActiveCitizen { get; set; }
        public int FK_PropertyType { get; set; }
    }
}

Controller: 控制器:

List<ActiveCitizenProperties> activeCitizenProperties = new List<ActiveCitizenProperties>();
activeCitizenProperties.Add(new ActiveCitizenProperties(1, 2));

The errors means what it does say: ActiveCitizenProperties constructor doesn't accept two parameters. 该错误表示它的含义:ActiveCitizenProperties构造函数不接受两个参数。 In the code given no constructor defined in the class at all. 在代码中,根本没有在类中定义构造函数。

You may use though: 您可以使用:

new ActiveCitizenProperties { FK_ActiveCitizen = 1, FK_PropertyType = 2 };

You may replace your code to: 您可以将代码替换为:

List<ActiveCitizenProperties> activeCitizenProperties = new List<ActiveCitizenProperties>();
activeCitizenProperties.Add(new ActiveCitizenProperties(){ FK_ActiveCitizen = 1, FK_PropertyType = 2 });

Your "auto-generated" class obviously doesn't contain a constructor that takes 2 arguments. 您的“自动生成”类显然不包含带有2个参数的构造函数。 If it has, it would be like this: 如果有,它将是这样的:

namespace ActiveCitizenSystemMimic.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ActiveCitizenProperties
    {
        public int FK_ActiveCitizen { get; set; }
        public int FK_PropertyType { get; set; }

        public ActiveCitizenProperties(int a, int b)
        {
            this.FK_ActiveCitizen = a;
            this.FK_PropertyType = b;
        }
    }
}

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

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