none
Code First - Repository and related entities RRS feed

  • Question

  • Hi

    I am creating a repository, it returns disconected entities from context

    the problem is with the related properties, because if I can create a new entity which is related to an existing, but EF create the new entity and also the related, when is should oly create the new

    the repository is defined

            public virtual void Insert(T entity)
            {
                using (DataBaseContext context = new DataBaseContext())
                {
                    context.Entry<T>(entity).State = EntityState.Added;
    
                    context.SaveChanges();
                }
            }

    I am indicating the state of new entity, not the related entities, the repository is generics, I don't know all related entities

    How I can indicate when the related entity must be created and when not?

    EF always create the related entities, but I am assigning a related entity with id

    greetings


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina

    Monday, November 5, 2012 2:57 PM

Answers

  • Leandro,

    The better way to work with code first, you need to implement a unit of work 

    Follow a example 

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    http://blogs.microsoft.co.il/blogs/gilf/archive/2010/06/21/revisiting-the-repository-and-unit-of-work-patterns-with-entity-framework.aspx


    Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC

    Thursday, November 8, 2012 4:42 PM

All replies

  • After assigning the id to the related entities, you also need to modify their states to 'modified'.

    Go go Doraemon!

    Wednesday, November 7, 2012 5:36 AM
  • Hi Dorado999

    Yes it's correct

    but the problem is the generic repository, if I'm implementing the Insert() generally for all entities, how I can indicate the state of associate entities ? remembers outside the repository I can't access to the EF context

    Or I should create specific Insert() method for each entity and indicate the state

    I was try if I assign the single properties the associate entity is not create, example

    I have the PersonEntity and it has the properties CompanyId (int) and Company (CompanyEntity) if I assign the CompanyId the PersonEntity is associated with the existing row in the Company Table, but If I create Find() and assign the Company property, the id exist but el Insert() create a new Company

    greetings


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina

    Thursday, November 8, 2012 2:40 PM
  • Leandro,

    The better way to work with code first, you need to implement a unit of work 

    Follow a example 

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    http://blogs.microsoft.co.il/blogs/gilf/archive/2010/06/21/revisiting-the-repository-and-unit-of-work-patterns-with-entity-framework.aspx


    Não esqueça de usar o componente </> na barra para posta seu código. Microsoft MCPD,MCTS,MCC

    Thursday, November 8, 2012 4:42 PM