Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
        }
share|improve this question
    
And? What is your question? –  David Jashi Jun 3 '13 at 9:42
    
@DavidJashi, How to add the new skill to that specific table. db.TECHNICAL_SKILLS_VALUES doesn't give me an ADD function –  Kriistiian Jun 3 '13 at 9:43
    
@DavidJashi, And i wrote my question in the comented code, did u even read whole question? –  Kriistiian Jun 3 '13 at 9:44
    
Well, this is not quite how we ask questions here, when we want to get an answer. –  David Jashi Jun 3 '13 at 9:45
    
@DavidJashi, My bad then, but back to the topic. How do i save the values? As i said, db.TECHNICAL_SKILLS_VALUES doesn't give me an add function –  Kriistiian Jun 3 '13 at 9:46

2 Answers 2

up vote 0 down vote accepted

Add the following line before db.SaveChanges();

db.TECHNICAL_SKILLS_VALUES.Add(dbSkill);

OR

db.AddToTECHNICAL_SKILLS_VALUES(dbSkill);
share|improve this answer
    
db.TECHNICAL_SKILLS_VALUES doesn't give me an add function :( –  Kriistiian Jun 3 '13 at 9:47
    
I've added another option. But both option usually available to use! –  sarwar026 Jun 3 '13 at 9:51
    
if none of the options works for you, then you have some other problem in initiating db, i guess –  sarwar026 Jun 3 '13 at 9:53
    
The second option worked, didn't even notice that there were an AddToTECHNICAL_SKILLS_VALUES function. Thanks! –  Kriistiian Jun 3 '13 at 9:57
    
@Kriistiian: Glad to know that it works! You are welcome –  sarwar026 Jun 3 '13 at 9:58

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

share|improve this answer
    
Hi there. Thanks for the answer, db.AddToTECHNICAL_SKILLS_VALUES(dbSkill); worked fine, but sarvar026 above you provide the answer. Thanks! –  Kriistiian Jun 3 '13 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.