简体   繁体   中英

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). I am getting the error: ActiveCitizenSystemMimic.Models.ActiveCitizenProperties does not contain a constructor that takes 2 arguments.

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. 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. 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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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