0

My question is simple, and wondering how this should be done properly.

Let's say I have 2 Models.

public class FirstModel
{
    public int FirstModel {get; set;}
    public string MyDetails {get; set;}
}

public class SecondModel
{
    public int SecondModel {get; set;}
    public int FirstModelID {get; set;}
    public string MyDetails {get; set;}

    public virtual <FirstModel> FirstModel {get; set;}
}

public class MyViewModel
{
    public string MyDetails {get; set;}
    public string MyDetails2 {get; set;}

    //Or Foreign key to other models. You get the idea.
}

In my Controller. I instantiated my ViewModel and pass it to the View.

Now I am worried about my workflow and I believe that there is a best way to Insert or even Update Data for the Enitity Model.

My work flow is Make an Instance of the 2 Model and either Add it to the dbContext. Which is fine for me.

But the troublesome is the Updating part.

[HttpPost]
public class ActionResult Save(MyViewModel vmData)
{
    FirstModel firstModel = new FirstModel();
    SecondModel secondModel = new SecondModel();

    firstModel.MyDetails = vmData.MyDetails;
    secondModel.MyDetails = vmData.MyDetails2;

    db.FirstModel.Add(firstModel);
    db.SaveChanges();

    secondModel.FirstModelID = firstModel.MyDetails;
    db.SecondModel.Add(secondModel);
    db.SaveChanges();
}

Now it seems simple in my part because there is only 1 property in FirstModel and 2 Properties to fill in SecondModel. But as the model goes, it is hard to manually fill them. Is there some technique like bind that can help me out with this? Any strategy for making it also available for Updating a dbContext?

while this Answer is helpful. enter link description here

This applies for the Model itself, wondering if I can use the same strategy for ViewModels. Thanks for checking my question.

Community
  • 1
  • 1
Aizen
  • 1,807
  • 2
  • 14
  • 28
  • 1
    Why call SaveChanges twice? This can all be done in a single save. and if both models are related you can already add them to the context. Also this line I think is incorrect: secondModel.FirstModelID = firstModel.MyDetails; – Martin Apr 20 '15 at 16:04
  • Are you sure I don't need to call SaveChanges twice? They are being posted from a ViewModel not Model. So when They're returned to the Controller, it is a ViewModel Instance not Binded to the Model itself. If it is a FirstModel to SecondModel, it is fine so save Once. That is what I believe. Correct me if I am wrong. – Aizen Apr 20 '15 at 16:12
  • The ViewModel has no relation with the database. As I see in your code you assign your VM properties to your entities, which is good. I just don't understand why you assign the firstModel.MyDetails to the ID? Again you can save both entities in a single SaveChanges() I also believe your ViewModel should be updated to have FirstModel and SecondModel as type instead of String. – Martin Apr 20 '15 at 16:16
  • Interesting. Can you please, Post an answer so I can try it? Really I am new to this ViewModel business and In my own way I can make it work, but that is my question is about. My work flow is tragic and finding ways to improve or at lease the proper way to do it. – Aizen Apr 20 '15 at 16:20
  • Well, no one is Answering, I will try your first suggestion to make it a type of class first and see what I will get. – Aizen Apr 20 '15 at 17:03

1 Answers1

0

The following code works assuming that there is a relationship between first and second model. You don't need to save changes twice.

[HttpPost]
public class ActionResult Save(MyViewModel vmData)
{
    FirstModel firstModel = new FirstModel();
    SecondModel secondModel = new SecondModel();

    firstModel.MyDetails = vmData.MyDetails;
    secondModel.MyDetails = vmData.MyDetails2;

    secondModel.FirstModel = firstModel;
    db.SecondModel.Add(secondModel);
    db.SaveChanges();
}

It will execute two queries in two queries in one transaction.

Yogiraj
  • 1,952
  • 17
  • 29