I have a problem I can not solve, so I am posting to the teachers to try to help me ... thanks
These are my data model
public partial class Cadastro
{
[Key, Column(Order = 0)]
public int cd_cadastro { get; set; }
public string razao { get; set; }
public virtual Cliente Cliente { get; set; }
public virtual Vendedor Vendedor { get; set; }
}
public partial class Cliente
{
[Key, Column(Order = 0)]
public int cd_cadastro { get; set; }
public string operacao_fiscal { get; set; }
}
public partial class Vendedor
{
[Key, Column(Order = 0)]
public int cd_cadastro { get; set; }
public decimal pr_comissao { get; set; }
}
--- relationship in Entities
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cadastro>().HasKey(s => new { s.cd_cadastro });
modelBuilder.Entity<Cadastro>().HasRequired(a => a.Cliente).WithOptional().WillCascadeOnDelete(true);
modelBuilder.Entity<Cadastro>().HasRequired(a => a.Vendedor).WithOptional().WillCascadeOnDelete(true);
}
So I created a class, because the user can include the information in a single view
public partial class CadCli
{
public virtual Cadastro Cadastro { get; set; }
public virtual Cliente Cliente { get; set; }
public virtual Vendedor Vendedor { get; set; }
}
In my view create (GET) is well @model CadCli
My Controller in this way:
[HttpPost]
public ActionResult Create (Cadastro cadastro, Cliente cliente, Vendedor vendedor)
{
// All fields of registration , usually comes filled
// Here is valid
if ( ModelState.IsValid )
{
cadastro.cd_cadastro = 1; // Test
db.Cadastro.Add ( cadastro) ;
// Does not come here because this FALSE
if ( cadastro.ind_cliente )
{
cliente.cd_cadastro = cadastro.cd_cadastro ;
db.Cliente.Add (client ) ;
}
// Here is the problem
db.SaveChanges ();
// Even without entering the line ind_cliente
// It does insert the cadastro ( OK )
// but makes an insert cliente
// And it should not , because I did not add
// look the line ind_cliente is SET TO FALSE
// Seems to me that would have to have something
// " Do not add the client in this CadCli "
}
In this case should not make the insert CLIENTE because it was not marked as a CLIENTE and not entered in line ind_cliente
But when ind_cliente line is true, it is normal This also happens with VENDEDOR or any other class within CadCli
Please how can I solve this without having to change my data model?