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

标签 c# .net asp.net-mvc compiler-errors

我不确定这里发生了什么。该模型是从数据库自动生成的,我看不到任何明显的东西(请注意,此时此刻是英国时间凌晨2:30,所以我可能半睡着了)。我收到错误消息:ActiveCitizenSystemMimic.Models.ActiveCitizenProperties不包含带有2个参数的构造函数。

模型:

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

最佳答案

您可以将代码替换为:

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

您的“自动生成”类显然不包含带有2个参数的构造函数。如果有,它将是这样的:
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;
        }
    }
}

关于c# - 不包含带有2个参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016475/

相关文章:

c# - 将 AAC 转换为 WAV

asp.net - 用户组的 session 超时不同

c# - 来自不同 Controller 的相同方法是否应该移动到 CommonController?

c# - 如何修复 "error CS0656: Missing compiler required member ' Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'"

c# - 绑定(bind)ListView不绑定(bind)

c# - Java 调试查看器工具

c# - 用户在部分 View 中登录并显示错误

c# - 您如何实际实现搜索契约(Contract)? (C#)

c# - 使用 C# 和 wpf 创建一个类似应用程序的停靠点

c# - 如果 C# 中的关键字未在系统类中定义,那么它们在 CLR 中的哪里?