Entity Framework - Detached objects

Last post 05-19-2009 5:20 AM by cgeers. 3 replies.

Sort Posts:

  • Entity Framework - Detached objects

    03-19-2009, 11:19 AM
    • Member
      point Member
    • ruben0
    • Member since 03-19-2009, 3:09 PM
    • Posts 4

    First I was using a singleton class with only one instance of my entities (my context) but that means every user on the site uses that same instance, so I changed it to use a new instance per controller.

    I've got my "UserController" and "CompanyController" (both used to update or get users/companies)

    A User object has a link to Company.

    What I do in my usercontroller:

    1    public void addUser(UserDTO userDTO) {
    2      User user = User.CreateUser(0, .....)
    3      user.Company = new CompanyController().getCompany(userDTO.Company.companyId);
    4      context.AddToUsers(user);
    5      context.SaveChanges();
    6    } 
    

     The problem I had here was the "Company" object was from another context (made in the companycontroller), so I thought, I'd just detach the Company object in CompanyController before I return it, so the "AddToUsers()" attaches it again.

    Is there a way to make this "AddToUsers" attach this? I can solve my problem by using this code:

    1    Company c = new Companycontroller().getCompany(userDTO.Company.companyId);
    2    context.AttachTo("Companies", c);
    3    user.Company = c;
    
      But I'd prefer a way it's done automatically, because I have a lot more objects and having to hardcode all these attachments will give a lot of messy code..
  • Re: Entity Framework - Detached objects

    03-24-2009, 5:35 AM
    • Member
      8 point Member
    • puruforum
    • Member since 03-24-2009, 5:14 AM
    • Posts 6

     Hi,

    Please explain more about it.

    if you are using singleton and doing check with user ==null then another part of the code needed otherwise make user to null after savechanges.

    and other solution is at the time of creation every time create new user.

    Thanks,

    Puru

    Puru
  • Re: Entity Framework - Detached objects

    04-21-2009, 3:13 AM
    • Participant
      930 point Participant
    • MisterFantastic
    • Member since 09-21-2008, 5:56 AM
    • Chennai
    • Posts 279

    Hi,

     You can use a single context itself. Noneed of using two context for two controller. That will solve the problem.

     Please let me know if you have problems.

    Thanks,

    Thani

    Tao of Jeet Kune Do
  • Re: Entity Framework - Detached objects

    05-19-2009, 5:20 AM
    • Contributor
      2,074 point Contributor
    • cgeers
    • Member since 02-21-2009, 2:41 AM
    • Belgium
    • Posts 275

    ruben0,

    Using a single instance for your context (= singleton) is not the best approach for an ASP.NET application because as you've said each user will wind up using that instance. Using a singleton context would work better in a WinForms application.

    Instantiating a new object for each "controller" class is a workable approach, but it not necessary either. A better approach would be to use a context per Http Request.

    I wrote a blog post about this a while back:

    http://cgeers.wordpress.com/2009/02/21/entity-framework-objectcontext/

    You could then pass in this context instance to the constructor of your controller classes. You could opt to use dependency injection for this...

    The following code is provided for illustrative purposes:

    1    public class MyController: IController
    2    {
    3      private IObjectContextProvider provider;
    4    
    5      public MyController(IObjectContextProvider provider)
    6      {
    7        this.provider = provider;
    8      }
    9    
    10     public void SomeMethod()
    11     {
    12       MyContext context = provider.GetContext();
    13       // ...
    14     }
    15   }
    
    Regards,
     Christophe 
      

     

     

    Please mark my reply as an answer if it helps you.

    Check out my blog
Page 1 of 1 (4 items)