If your models wear Guids
, the solution becomes natural.
Suppose the following models:
public class ModeloPrincipal
{
[Key]
public Guid ModeloPrincipalId {get; set; }
}
public class ModeloAba1
{
[Key]
public Guid ModeloAba1Id {get; set; }
public Guid ModeloPrincipalId {get; set; }
public virtual ModeloPrincipal ModeloPrincipal { get; set; }
}
public class ModeloAba2
{
[Key]
public Guid ModeloAba2Id {get; set; }
public Guid ModeloPrincipalId {get; set; }
public virtual ModeloPrincipal ModeloPrincipal { get; set; }
}
public class ModeloAba3
{
[Key]
public Guid ModeloAba3Id {get; set; }
public Guid ModeloPrincipalId {get; set; }
public virtual ModeloPrincipal ModeloPrincipal { get; set; }
}
When the main model already exists, you use a temporary key to reference in the creation:
public ActionResult Create()
{
var modeloPrincipal = new ModeloPrincipal();
modeloPrincipal.ModeloPrincipalId = Guid.NewGuid();
modeloPrincipal.ModeloAba1 = new ModeloAba1 {
modeloPrincipalId = modeloPrincipal.ModeloPrincipalId;
};
modeloPrincipal.ModeloAba2 = new ModeloAba2 {
modeloPrincipalId = modeloPrincipal.ModeloPrincipalId;
};
modeloPrincipal.ModeloAba3 = new ModeloAba4 {
modeloPrincipalId = modeloPrincipal.ModeloPrincipalId;
};
return View(modeloPrincipal);
}
The generation of the keys is all at the discretion of the programmer. The bank only receives the information. There is no concern with primary key generation IDENTITY
and no need to work with temporary keys.
When it comes to saving a single SaveChanges()
does everything.
Now, if your models use primary keys int
with IDENTITY
, you will have save the main registration already in the first tab change, along with the first tab registration, unless the user cancels the action already in the first tab. I consider that for a system using Angularjs this is very bad.