4
I have a small application in C#, WPF and EF6.
I’m using Code First, but I’m having a hard time creating a relationship. Look at my code, of the classes I want to relate: I’m editing for the final version, in case anyone has the same doubt.
public class Caixa
{
[Key]
public int CaixaId { get; set; }
public DateTime DataAbertura { get; set; }
public DateTime DataFechamento { get; set; }
public Boolean Aberto { get; set; }
public Decimal Troco { get; set; }
public Decimal TotalRetiradas { get; set; }
public Decimal TotalEntradas { get; set; }
public Decimal Total { get; set; }
public Decimal TotalEmCaixa { get; set; }
public String Observacoes { get; set; }
public Usuario Responsavel { get; set; }
public Caixa()
{
DataAbertura = DateTime.Now;
}
}
public class Usuario
{
[Key]
public int UsuarioId { get; set; }
[Required, Index(IsUnique=true)]
public String Login { get; set; }
[Required]
public String Password { get; set; }
public Boolean Administrador { get; set; }
}
public class Contexts: DbContext
{
//entidades
public DbSet<Usuario> Usuarios { get; set; }
public DbSet<Produto> Produtos { get; set; }
public DbSet<Caixa> Caixas { get; set; }
public Contexts()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Contexts>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
}
The code you save is as follows::
database.Caixas.Add(NovoCaixa);
NovoCaixa.Responsavel = database.Usuarios.Single(u => u.UsuarioId == UsuarioAtivo.UsuarioId);
database.SaveChanges();
Whenever I try to save the entity Box, I see this mistake:
System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into a unique index. [ Table name = Usuario, Constraint name = IX_Login ]
I suspect he is trying to insert the user, but it is not what I want, because it already exists.
Can anyone help me? I am doing this project to learn EF.
Thanks for the answer, I already did and it didn’t work. What I see in my mind is that at the moment of saving, it is inserting the User instance as a new tuple, causing the error. That thought makes sense?
– Vinicius Maboni
Do it. Like you’re doing to save?
– Leonel Sanches da Silva
database.Caixas.Addorupdate(box); database.Savechanges(); databse inherits from context and has all the Dbsets from the system
– Vinicius Maboni
@Viniciusmaboni I updated the answer.
– Leonel Sanches da Silva
It didn’t work out buddy. Actually I want the box to always be inserted as new, but the user is who should exist previously. Following your suggestion, it inserts a new box for each user id, so it doesn’t keep track of what I need.
– Vinicius Maboni
No. According to my suggestion,
Caixa
is inserted only if anotherCaixa
with the same name does not exist. If it exists, it updates the box record that has the corresponding name. It is like calling aupdate
in SQL.– Leonel Sanches da Silva
What code do you use to insert a new one
Usuario
?– Leonel Sanches da Silva
That’s not what I need. I need that, if it exists it create the same way, but the box with different id. Nothing prevents a user from opening several boxes, it is a one-to-Many relation. The entry code is very simple: database.Caixas.Addorupdate(box); database.Savechanges();
– Vinicius Maboni
Please edit your question and place the codes there.
– Leonel Sanches da Silva
@Viniciusmaboni I made another edition in the reply.
– Leonel Sanches da Silva
Wow! What a class buddy! Thank you very much. I think I was mixing patterns in my head, don’t you? .
– Vinicius Maboni