How to add another constructor with parameter in linq class (table)

I am using LINQ to SQL in an ASP.NET project. When inserting a table, I need to convert the values ​​to a specific table object and I need to insert.

For this, I created a new constructor on this table with a parameter so that I can assign my value to this table object, assign functionality, but on insert ( obj.TS_Questions.InsertOnSubmit(mytableobject);

) I get a null exception.

my code ::

default constructor for my table

public TS_Question()
        {
            this._TS_Options = new EntitySet<TS_Option>(new Action<TS_Option>(this.attach_TS_Options), new Action<TS_Option>(this.detach_TS_Options));
            this._TS_QuestGroups = new EntitySet<TS_QuestGroup>(new Action<TS_QuestGroup>(this.attach_TS_QuestGroups), new Action<TS_QuestGroup>(this.detach_TS_QuestGroups));
            this._TS_QuestRecords = new EntitySet<TS_QuestRecord>(new Action<TS_QuestRecord>(this.attach_TS_QuestRecords), new Action<TS_QuestRecord>(this.detach_TS_QuestRecords));
            this._TS_Admin = default(EntityRef<TS_Admin>);
            this._TS_LevelType = default(EntityRef<TS_LevelType>);
            this._TS_OptionTypeLT = default(EntityRef<TS_OptionTypeLT>);
            OnCreated();
        }

      


the constructor I created

    public TS_Question(Guid Quest_QuestIDBL, string Quest_NameBL, Nullable<Guid> Quest_OptionTypeIDBL, Guid Quest_AdminIDBL, Guid Ques_LevelIDBL, int Quest_TimeBL, int Quest_MarkBL, string Qest_ExplanationBL, Nullable<bool> Qest_IsMultipleAnswerBL)
    {

        this._TS_Options = new EntitySet<TS_Option>(new Action<TS_Option>(this.attach_TS_Options), new Action<TS_Option>(this.detach_TS_Options));
        this._TS_QuestGroups = new EntitySet<TS_QuestGroup>(new Action<TS_QuestGroup>(this.attach_TS_QuestGroups), new Action<TS_QuestGroup>(this.detach_TS_QuestGroups));
        this._TS_QuestRecords = new EntitySet<TS_QuestRecord>(new Action<TS_QuestRecord>(this.attach_TS_QuestRecords), new Action<TS_QuestRecord>(this.detach_TS_QuestRecords));
        this._TS_Admin = default(EntityRef<TS_Admin>);
        this._TS_LevelType = default(EntityRef<TS_LevelType>);
        this._TS_OptionTypeLT = default(EntityRef<TS_OptionTypeLT>);
        OnCreated();

        this._Quest_QuestID = Quest_QuestIDBL;
        this._Quest_Name = Quest_NameBL; 
        if (Quest_OptionTypeIDBL != null)
        {
            this._Quest_OptionTypeID = Quest_OptionTypeIDBL;
        }
        this._Quest_AdminID = Quest_AdminIDBL;
        this._Ques_LevelID = Ques_LevelIDBL;
        this._Quest_Time = Quest_TimeBL;
        this._Quest_Mark = Quest_MarkBL;
        this._Qest_Explanation = Qest_ExplanationBL;
        this._Qest_IsMultipleAnswer = Qest_IsMultipleAnswerBL;

    }

      

Please help me to solve this problem.

+1


a source to share


2 answers


To be honest, I didn't look too deep, but it looks like OnCreated sits a little further north ... You probably want to call it after you're done setting up your variables. Other than that, I would say that you initialize everything correctly in the method that calls the constructor.



+2


a source


You can call the default constructor like this, it works great for me:

 public partial class MyClass
{
    public MyClass(string fieldValue1,int fieldValue2)
        : this()
    {
        this.field1= fieldValue1;
        this.field2 = fieldValue2;
    }
}

      



If that's a trick, you can read more about using constructors in C # here .

+2


a source







All Articles