-1

I want to add new records to an related table. I got three tables: enter image description here

I want to add new technical_skill_value to that table. This is my code right now:

        using (var db = new KnowItCvdbEntities())
        {
            var dbSkill = new TECHNICAL_SKILLS_VALUES
            {
                technical_skill_value_id = new Random().Next(),
                skill_name = TextBoxTechSkill.Text,
                skill_type = DropDownListTechSkill.SelectedItem.Text
            };

            //I want to add the new skill to my TECHNICAL_SKILLS_VALUE table. But I really don't get it how to make it.     
            db.SaveChanges();
        }
krisal
  • 601
  • 6
  • 19

2 Answers2

0

Add the following line before db.SaveChanges();

db.TECHNICAL_SKILLS_VALUES.Add(dbSkill);

OR

db.AddToTECHNICAL_SKILLS_VALUES(dbSkill);
sarwar026
  • 3,773
  • 3
  • 25
  • 36
0

I suspect there is an issue surrounding your naming... e.g. an entity for people would be named Person, the entity set would be called Persons. Yet in your code you create a new entity set and not a new entity (according to naming).

Notice the pluralisation... your actually creating an entity set?

I would expect something more along the lines of:

        TECHNICAL_SKILLS_VALUE dbSkill = new TECHNICAL_SKILLS_VALUE
        {
            technical_skill_value_id = new Random().Next(),
            skill_name = TextBoxTechSkill.Text,
            skill_type = DropDownListTechSkill.SelectedItem.Text
        };

        db.AddToTECHNICAL_SKILLS_VALUES(dbSkill) 
        db.SaveChanges();

Try:

db.AddToTECHNICAL_SKILLS_VALUES(dbSkill);
db.SaveChanges();

Try to attach the object:

db.TECHNICAL_SKILLS_VALUES.AddObject(dbSkill);
db.SaveChanges();

read this for reference and examples : clicky link

Community
  • 1
  • 1
Paul Zahra
  • 9,183
  • 7
  • 51
  • 72
  • Hi there. Thanks for the answer, db.AddToTECHNICAL_SKILLS_VALUES(dbSkill); worked fine, but sarvar026 above you provide the answer. Thanks! – krisal Jun 03 '13 at 10:39