0

suppose in entity framework, i have the following models:

public class User
{
   public int UserId { get; set; }
   public string Name { get; set; }

   public virtual ICollection<Item> Items { get; set; }
}

public class Item
{
   public int ItemId { get; set; }
   public string Name { get; set; }

   public virtual User User{ get; set; }
}

that is a user can have many items, but an item has only one user. Now in asp web api i create one user and one item with a POST request: for example for the user i have the UsersController with this method:

public IHttpActionResult PostUser(User user)
{
   if (!ModelState.IsValid)
   {
      return BadRequest(ModelState);
   }

   db.Users.Add(user);
   db.SaveChanges();

   return CreatedAtRoute("DefaultApi", new { id = user.Id }, user);
}

Now if i want update the created user, assigning to him the created item, how can i achieve it? I must call the PUT method on the UsersController right? What's the request body json object?

Thanks

3 Answers 3

2

There's an usual practice for ASP.NET MVC to send an object to action and update entities like that:

public IHttpActionResult Edit(User user)
{
   try
   {
      if (ModelState.IsValid)
      {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
      }
   }
   catch (Exception e)
   {

   }
}

but if you have a complex object, that contains some sort of list of another complex objects stored in DB, Entity Framework will set the EntityState.Modified to all entity graph and items, stored in list will be saved again.

So, in my practice, I send an object without included lists and update action is looks like this:

public IHttpActionResult Edit(User user)
    {
       try
       {
          User original = db.FirstOrDefault(u=>u.Id==user.Id);
          original.Name = user.Name;

          if (ModelState.IsValid)
          {
             db.Entry(original).State = EntityState.Modified;
             db.SaveChanges();
          }
       }
       catch (Exception e)
       {

       }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

ok but if i want assign an item to the user? can i call the edit method on the user controller?
you can check if there's items in passed object if (user.Items) original.Items = user.Items
0

I believe you touch more general problem which is how to use Entity Framework in disconnected scenario (n-tier architecture). Basic problem in that scenario is that you lose nice feature of EF which is automatic change tracking. As a consequence of this you have to implement it by yourself. In practice it means that you have to set appropriate EntityState for each object in you graph before you execute SaveChanges() method.

You can find a couple of articles and discussions covering this topic which can help you to design your architecture. e.g:

http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/

My experience is based on EF 4 but I looking at the link above I think that EF still suffers in this area:

http://entityframework.codeplex.com/workitem/864

Comments

0

You might be interested in using the open source N-Tier Entity Framework which uses Entity Framework on server-side and generates the entire infrastructure for building an n-tier architecture based on WCF including an EF-like API on client-side. However, this framework requires your client to use WCF instead of WebAPI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.