1

I'm new to ASP.NET MVC and want to create a small order management tool. My database contains the tables Orders and Articles (and a few other ones), and I generated an EF Model from my database, so I can use the full power of the EF mappings (e.g. db.Orders.Articles)

My two main relations which I'm concerned about are Orders and Articles.

  • An order can have many articles
  • An article can only belong to one order.

I've created an OrdersController with an Create action to create an order:

        //
    // GET: /Orders/Create

    public ActionResult Create()
    {
        Order order = new Order()
        {
           // filling some order columns, e.g. date
        };

        Article article = new Article()
        {
            // ... article columns
        };

        order.Articles.Add(article);

        return View(order);

    } 

    //
    // POST: /Orders/Create

    [HttpPost]
    public ActionResult Create(Order order)
    {
        // i know i should care more about error handling, but now ommit it
        db.Orders.AddObject(order);
        db.SaveChanges();

        return RedirectToAction("index");
    }

So I'm directly binding an EF Object to a view (read somewhere not to do that and use a view model instead, but don't really know what that view model should look like)

My view contains the Order form as well as the article form (because i want to create a order and articles at the same time and not seperate). I used these greate EditorFor Methods to do that.

And now to my problem: If i hit the submit button, the app crashes as soon as it comes to the HttpPost Create Method (when mapping the order) with this error message:

Error Message: The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph.

If i hit continue in VS2010, it will complete saving the order - so my question is how to solve this problem in a reliable way.

Thanks in advance and sorry for that long story :)

1
  • 2
    My advice would be to not use the EF object as your model and to create a ViewModel instead (as you mention in your question). The ViewModel should contain all the fields you are going to show/edit in the view, so initially you might just create a copy of your entity objects and name them OrderViewModel and ArticleViewModel and the OrderViewModel will contain a list of ArticleViewModels. In the Create action you just have to make sure you copy all the data from the viewmodels into the EF objects (this process can be automated - take a look at Automapper). Commented May 27, 2011 at 12:55

1 Answer 1

1

I solved my problem now by using a separate ViewModel like @Yakimych advised me. However I did not copy all the attributes from the EF models, instead I just refer to them. My ViewModel looks like this:

public class NewOrderViewModel {
  public Order { get; set; }
  public List<Article> { get; set; }
}
Sign up to request clarification or add additional context in comments.

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.