0
Good afternoon. I am studying the Entityframework and I am not managing to do something that seems to be simple but will not. ( I wanted to make a 1:N relationship between a table for a customer that may have multiple quotes, but quotes can only have one customer.
This is my class Client:
public class Cliente
{
[Key]
public int ID { get; set; }
public double CPF { get; set; }
public string Nome { get; set; }
//public Cotacao Cotacao { get; set; }
public ICollection<Cotacao> Cotacao { get; set; }
//public virtual IEnumerable<Cotacao> Cotacoes { get; set; }
public Cliente(double cpf, string nome, ICollection<Cotacao> cotacao)
{
CPF = cpf;
Nome = nome ?? throw new ArgumentNullException(nameof(nome));
Cotacao = cotacao ?? throw new ArgumentNullException(nameof(cotacao));
}
public Cliente(double cpf, string nome)
{
CPF = cpf;
Nome = nome ?? throw new ArgumentNullException(nameof(nome));
}
}
This is my class Quotation:
public class Cotacao
{
[Key]
public int ID { get; set; }
public virtual Cliente Cliente { get; set; }
public string Idade { get; set; }
public string Genero { get; set; }
public string Marca { get; set; }
public string Modelo { get; set; }
public string AnoFabricacao { get; set; }
public string AnoModelo { get; set; }
public Cotacao(Cliente cliente, string idade, string genero, string marca, string modelo, string anoFabricacao, string anoModelo)
{
Cliente = cliente;
Idade = idade;
Genero = genero;
Marca = marca;
Modelo = modelo;
AnoFabricacao = anoFabricacao;
AnoModelo = anoModelo;
}
public Cotacao(string idade, string genero, string marca, string modelo, string anoFabricacao, string anoModelo)
{
Idade = idade;
Genero = genero;
Marca = marca;
Modelo = modelo;
AnoFabricacao = anoFabricacao;
AnoModelo = anoModelo;
}
}
This is my Context:
public partial class ContextDB : DbContext
{
public DbSet<Cliente> Clientes { get; set; }
public DbSet<Cotacao> Cotacoes { get; set; }
public ContextDB() : base("name=ContextDB")
{
}
}
And that’s how I’m creating saving the data in the bank:
if (!string.IsNullOrEmpty(cpf))
{
using (var ctx = new ContextDB())
{
Cliente cliente = new Cliente(double.Parse(cpf), nome);
Cotacao cotacao = new Cotacao(cliente, idade, genero, marca, modelo, anoFabricacao, anoModelo);
if (cliente.CPF == cotacao.Cliente.CPF) // Sem esse if, também não vai.
{
ctx.Cotacoes.Add(cotacao);
ctx.SaveChanges();
}
else
{
ctx.Clientes.Add(cliente);
ctx.Cotacoes.Add(cotacao);
ctx.SaveChanges();
}
}
}
But every time he creates a new client and does not associate the contract with the client.
I imagine the problems are in creating two objects, but I don’t know how I would add an existing object in the creation of the other. What I’d be doing wrong?
Hey, Robson, thanks for your help, but I still have the same problem. New customer registrations are still generated and quotations continue to be created with these "new" customer Ids, you would have more hints than could be wrong?
– R. Souza
R.Souza edited the answer there, worked right in the tests I did here. See if it works for you too.
– Robson Silva
If you want to leave the property Clienteid being passed in the constructor also just add it together in any of the constructors, and do not delete the empty constructor option, otherwise at the time of bringing the data you will not be able.
– Robson Silva
Robson, thanks for your help, it really worked out the way you showed it. I added the information you gave me and looked for Fluentapi, it helped me a lot. E I used the information from this link link to search for the first person in the bank with the ID/CPF that enters as information. Thank you very much again, I knew I was missing some things, but I will try to study more to understand all of the Entity, hugs.
– R. Souza
That’s good that it worked. Entity really is very good and dominalo will give you a good advantage in the constructions of your applications. Flw good study there.
– Robson Silva