If your templates use Guids
, the solution becomes natural.
Suppose the following templates:
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 parent template already exists, you use a temporary key to reference 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 entirely at the discretion of the programmer. The bank only receives the information. There is no concern with primary key generation IDENTITY
and there is no need to work with temporary keys.
When saving, a single SaveChanges()
does everything.
Now, if your models use int
primary keys with IDENTITY
, you'll have to save the master record already on the first tab change, along with the first tab record, unless the user cancels the action already in the first tab. I think for a system using AngularJS this is pretty bad.