3
Good morning, I have asked a similar question, I am trying to complement this see with more information and starting from the most basic.
I have the following structure:
public class Artista
{
public Artista()
{
}
public int ArtistaId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Site { get; set; }
public string Descricao { get; set; }
public virtual Endereco Endereco { get; set; }
public DateTime DataCadastro { get; set; }
public DateTime DataAtualizacao { get; set; }
}
public class Endereco
{
public Endereco()
{
Municipio = new Municipio();
}
public int EnderecoId { get; set; }
public string Logradouro { get; set; }
public string Numero { get; set; }
public string Bairro { get; set; }
public string Cep { get; set; }
public int MunicipioId { get; set; }
public virtual Municipio Municipio { get; set; }
}
public class Municipio
{
public Municipio()
{
}
public int MunicipioId { get; set; }
public string Nome { get; set; }
public string Cep { get; set; }
}
The address entity should be optional, during registration I can inform the address or not.
Configuration Fluent API
public class ArtistaConfiguration : EntityTypeConfiguration<Artista>
{
public ArtistaConfiguration()
{
HasKey(a => a.ArtistaId);
Property(a => a.Nome)
.IsRequired();
Property(a => a.Email)
.HasMaxLength(150);
}
public class EnderecoConfiguration : EntityTypeConfiguration<Endereco>
{
public EnderecoConfiguration()
{
HasKey(x => x.EnderecoId);
HasRequired(m => m.Municipio)
.WithMany()
.HasForeignKey(m => m.MunicipioId);
Property(m => m.Cep)
.IsFixedLength()
.HasMaxLength(9)
.HasColumnType("char");
}
}
The artist’s registration is done as follows:
public void Add(Artista obj)
{
ValidaEndereco(obj);
Db.Set<Artista>().Add(obj);
Db.SaveChanges();
}
private void ValidaEndereco(Artista artista)
{
var endereco = artista.Endereco;
if (string.IsNullOrEmpty(endereco.Logradouro)
& string.IsNullOrEmpty(endereco.Numero)
& string.IsNullOrEmpty(endereco.Bairro))
{
artista.Endereco = null;
}
else
{
artista.Endereco.Municipio = null;
}
}
The update:
public void Update(Artista obj)
{
AtualizaEndereco(obj);
Db.Entry(obj).State = EntityState.Modified;
Db.SaveChanges();
}
private void AtualizaEndereco(Artista artista)
{
var endereco = artista.Endereco;
if (string.IsNullOrEmpty(endereco.Logradouro)
& string.IsNullOrEmpty(endereco.Numero)
& string.IsNullOrEmpty(endereco.Bairro))
{
artista.Endereco = null;
}
else
{
artista.Endereco.Municipio = null;
endereco.Municipio = null;
Db.Entry(endereco).State = EntityState.Modified;
}
}
In this case, when registering an artist and not registering the address, works correctly and does not generate any error, but when I edit this registration to add an address the following error is shown:
An Exception of type 'System.Data.Entity.Infrastructure.Dbupdateconcurrencyexception' occurred in Entityframework.dll but was not handled in user code
Additional information: Store update, Insert, or delete statement affected an Unexpected number of Rows (0). Entities may have been modified or Deleted Since entities Were Loaded. See http://go.microsoft.com/fwlink/? Linkid=472540 for information on understanding and Handling optimistic concurrency exceptions.
As already mentioned in another post, I always programmed with ADO and Stored Procedures, and I always heard that I was wasting time, that Entity Framework has a very big productivity gain, but I feel that I have totally lost control of the application. I’m totally lay with EF, I’m getting beaten up to do things "idiots" sometimes things that were working stop working without much explanation, this is an application I’m doing to study and I can’t get out of the register, when one part runs another part stops working...
If anyone needs the source to verify I can provide also...
I understand what you are going through. Normally the transition is not very simple, but it is important not to guess how to do things. I will answer.
– Leonel Sanches da Silva