I have a controller, “Hardware” which has a “Create” action.
The model looks like:
namespace Inventory.Models
{
public class HardwareModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string UPDATED { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Brand")]
public string BRAND { get; set; }
[Required]
[DataType(DataType.Text)]
[Display(Name = "Model")]
public string MODEL { get; set; }
}
}
This works just fine in conjunction with the /Hardware/Create/ controller which was generated (create action uses Razor template), however, when a new item is created, I would like to also update another table which keeps track of which item was updated (ID from HardwareModel), and when it was updated. I am able to get both of these fields I need – but my question is, how do I extend this model to create a new record in 2 seperate tables? (using MVC4)
That sounds like a candidate method for a service layer.
If your business logic dictates that creating a HardwareModel entity also requires creating two other entities, enforce that rule in a service method and unit test that the create methods for those two entities are called.