1
protected void ButtonAddDatabase_Click(object sender, EventArgs e)
{
    using (KnowItCvdbEntities db = new KnowItCvdbEntities())
    {
        SPWeb theSite = SPControl.GetContextWeb(Context);
        SPUser theUser = theSite.CurrentUser;
        string strUserName = theUser.LoginName;

        var theEmplDbSkill = (
                        from p
                        in db.EMPLOYEES
                        where p.username == strUserName
                        select p).FirstOrDefault();

        _emp = theEmplDbSkill;

        if (_emp != null)
        {                        
            foreach (var vItem in ListBoxDatabases.Items)
            {
                if (ValueAlreadyInListDb(vItem))
                    return;
            }                        

      //Get existing skilllevel from db
      var skillLevel = (from sL in db.TECHNICAL_SKILL_LEVEL 
                        where sL.technical_skill_level_id == Convert.ToInt32(DropDownListDB.SelectedValue)
                        select sL).FirstOrDefault();

       //Get existing skillvalue from db
        var skillValue = (from sV in db.TECHNICAL_SKILLS
                         where sV.technical_skill_id == Convert.ToInt32(RadioButtonListDatabase.SelectedValue)
                         select sV).FirstOrDefault();

       //Adding to employees_technical_skills table
       var empSkill = new EMPLOYEES_TECHNICAL_SKILLS
       {
            technical_skill_id = Convert.ToInt32(DropDownListDB.SelectedItem.Value),
            TECHNICAL_SKILLS = skillValue,
            technical_skill_level_id = Convert.ToInt32(RadioButtonListDatabase.SelectedItem.Value)
            TECHNICAL_SKILL_LEVEL = skillLevel,
            employee_id = _emp.employee_id
        }

            _emp.EMPLOYEES_TECHNICAL_SKILLS_Add(empSkill);
            db.SaveChanges();

            _emp.EMPLOYEES_TECHNICAL_SKILLS.Add(empSkill);
            db.SaveChanges();

        }                
    }
}    

现在我正在填充下表: 在此处输入图像描述

这是我的桌子: 在此处输入图像描述

但我不断收到错误:

LINQ to Entities 无法识别方法“Int32 ToInt32(System.String)”方法,并且该方法无法转换为存储表达式。

问候,克里斯蒂安。

4

1 回答 1

0

看起来实体框架无法转换Convert.ToInt32()为 SQL 表达式。我会int先将单选按钮和下拉列表值转换为内存,然后将它们传递给查询:

int TechnicalSkillLevelID = int.Parse(DropDownListDB.SelectedValue);
int TechnicalSkillID = int.Parse(RadioButtonListDatabase.SelectedValue);
//Get existing skilllevel from db
      var skillLevel = (from sL in db.TECHNICAL_SKILL_LEVEL 
                        where sL.technical_skill_level_id == TechnicalSkillLevelID)
                        select sL).FirstOrDefault();

       //Get existing skillvalue from db
        var skillValue = (from sV in db.TECHNICAL_SKILLS
                         where sV.technical_skill_id == TechnicalSkillID)
                         select sV).FirstOrDefault();

       //Adding to employees_technical_skills table
       var empSkill = new EMPLOYEES_TECHNICAL_SKILLS
       {
            technical_skill_id = skillValue.technical_skill_id,
            TECHNICAL_SKILLS = skillValue,
            technical_skill_level_id = skillLevel.technical_skill_level_id)
            TECHNICAL_SKILL_LEVEL = skillLevel,
            employee_id = _emp.employee_id
        }

此外,由于您已经撤回了技能等级和技能值,因此无需从下拉/单选按钮列表中获取值。只需使用skillValueandskillLevel对象。

于 2013-03-28T17:42:00.503 回答