【问题标题】:How To add another constructor with parameter in linq class(Table)如何在 linq 类(表)中添加另一个带参数的构造函数
【发布时间】:2009-05-06 08:33:12
【问题描述】:

我在 ASP.NET 项目中使用 LINQ to SQL。在插入表格时,我需要将值转换为特定的表格对象并且我需要插入。

为此,我在该表中创建了一个带有参数的新构造函数,以便我可以将我的值分配给该表对象,分配功能正在工作,但是在插入时 (obj.TS_Questions.InsertOnSubmit(mytableobject);) 我得到空异常。

我的代码::

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

我创建的构造函数

    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;

    }

请帮我解决这个问题

【问题讨论】:

标签: c# asp.net linq linq-to-sql


【解决方案1】:

老实说,我并没有看得太深,但看起来 OnCreated 位于偏北一点...您可能想在完成变量设置后调用它。除此之外,我会说确保您正确初始化了调用构造函数的方法中的所有内容。

【讨论】:

    【解决方案2】:

    你可以像这样调用默认构造函数,它对我来说很好用:

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

    如果这样做可以解决问题,您可以阅读有关在 C# 中使用构造函数的更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-11
      • 2017-12-31
      • 2023-03-16
      • 1970-01-01
      • 2019-01-14
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多