locked
EF "Master/Detail" RRS feed

  • Question

  •  

    Hello,

     

    I have an object Master e other Detail, they linked by association.

    So how do I add a new item in Detail in an object Master already existing?

    Wednesday, August 27, 2008 11:09 AM

Answers

  • I am not sure to understand what you want to do but you can add a new detail entity instance to your ObjectContext and set his navigation property to your existing Master entity instance. If you haven't it yet in your ObjectContext, you can use a (Try)GetObjectByKey or a LINQ Query or a esql query to get it.

    For example, with Northwind you can do:

    Code Snippet

    using (var context = new NorthwindEntities())

    {

      context.AddToProducts(new Product { ProductID = 11181981, ProductName = "NewProduct", Category = context.First(c => c.CategoryID == 1) } );

      context.SaveChanges();

    }

     

     

    Is it what you want to know?

    Wednesday, August 27, 2008 11:46 AM

All replies

  • I am not sure to understand what you want to do but you can add a new detail entity instance to your ObjectContext and set his navigation property to your existing Master entity instance. If you haven't it yet in your ObjectContext, you can use a (Try)GetObjectByKey or a LINQ Query or a esql query to get it.

    For example, with Northwind you can do:

    Code Snippet

    using (var context = new NorthwindEntities())

    {

      context.AddToProducts(new Product { ProductID = 11181981, ProductName = "NewProduct", Category = context.First(c => c.CategoryID == 1) } );

      context.SaveChanges();

    }

     

     

    Is it what you want to know?

    Wednesday, August 27, 2008 11:46 AM
  • Hello MatthieuMezil,

     

    I posted the problem in other thread, but I'll try to explain it here.

     

    I have an object called objClient that contains a collection objContact as property.

     

    As I add new CLIENT and for example  two new CONTACTS.. it works.

    But whether I try to add a new CONTACT after have already saved a CLIENT

    the error occurs saying that already exists an EntityKey, but the EntityKey

    that exists belong CLIENT and I can't attach it becouse it is new!

    I don't know what to do.

     

    Just to know I'm using WCF.

    Wednesday, August 27, 2008 12:18 PM
  • Hi MatthieuMEZIL,

     

    I did some like that and worked.

     

    IEnumerator<eContato> ieContato;

     

    ieContato = objPJ.eContato.GetEnumerator();

     

    for (int i = 0; i < objPJ.eContato.Count; i++)

    {

    ieContato.MoveNext();

    if (ieContato.Current.EstadoDoObjeto == System.Data.EntityState.Modified)

    {

    if (ieContato.Current.ID == 0)

    {

    eContato ec = new eContato();

    ec.ePessoaJuridica.EntityKey = objPJ.EntityKey;

    ec.ID = ieContato.Current.ID;

    ec.Descricao = ieContato.Current.Descricao;

    ec.eTipoDeContato.ID = ieContato.Current.eTipoDeContato.ID;

    ec.EstadoDoObjeto = System.Data.EntityState.Unchanged;

    dm.AddToesContato(ec);

    }

    else

    {

    dm.AttachTo("esContato", ieContato.Current);

    dm.ObjectStateManager.GetObjectStateEntry(ieContato.Current).SetModified();

    dm.Refresh(System.Data.Objects.RefreshMode.ClientWins, ieContato.Current);

    }

    }

     

    But now I have other problem, I'll post it into a new thread.

    Tuesday, September 2, 2008 11:20 AM