none
where is addobject in entity framework when only find create? RRS feed

  • Question

  • Project_Cert_TestModel.Project.CreateProject only have createproject

    but a book have AddObject like the following,

    ctx.Customers.AddObject(customer);


    Sigma : Market always changed. God bless you.
    Monday, October 3, 2011 2:01 AM

All replies

  • Hi, 

    There is no AddObject available in DbSet , you can only call Add.

    ctx.Customers.Add(customer);

     

    Is this is the one you are looking for?


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Monday, October 3, 2011 2:34 AM
  • there is only Project_Cert_TestModel.Project.CreateProject() be chosen, no add, i am using edmx file, not dataset


    Sigma : Market always changed. God bless you.
    Monday, October 3, 2011 6:28 AM
  • Hello,

    I attached a simple code snippet as shown. (ctx was the instance of ObjectContext)

            private void button1_Click(object sender, EventArgs e)
            {            
                //add record
                Parent parent = new Parent
                {
                    Price = 5,
                    startDate = 6
                };
                ctx.AddToParents(parent);
                ctx.SaveChanges();
    
                //AddObject into current context
                Parent parent = new Parent
                {
                    Price = 7,
                    startDate = 9
                };
                ctx.Parents.AddObject(parent);
                ctx.SaveChanges();
            }

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 4, 2011 5:37 AM
  • Not know where AddToParents come from
    Sigma : 2*n trials, from learning to discover
    Tuesday, October 4, 2011 9:55 AM
  • AddToParents method was focused on adding a new object to the Parents Entity, but this method was deprecated instead I recommand you use the second Add method associated with ObjectSet<T>. Please double check the second code snippets on my previous reply.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 5, 2011 4:55 AM