views:

59

answers:

1

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys.

this is the code that i am using, I would appreciate any comments and suggestion on this.

using (KnowledgeShareEntities entities = new KnowledgeShareEntities())
            {
                Questions question = new Questions();
                question.que_title = questionTitle;
                question.que_question_text = questionText;
                question.que_number_of_views = 0;
                question.que_is_anonymous = isAnonymous;
                question.que_last_activity_datetime = DateTime.Now;
                question.que_timestamp = DateTime.Now;
                question.CategoriesReference.Value = Categories.CreateCategories(categoryId);
                question.UsersReference.Value =  Users.CreateUsers(userId);
                entities.AddToQuestions(question);
                entities.SaveChanges();

                return question.que_id;
            }
A: 

You should use something like

question.UsersReference.EntityKey =  new EntityKey("MyEntities.Users",
    "ID", userId);

You don't have to have User object to set up foreign key, just use ID.

LukLed
Thanks for the info!
richard hartz