Lambda, add new item from the MVC 5 list

Asked

Viewed 60 times

0

I have a model where a client has one or many Systems, and a System has one or many Customers: N:M

I’m having a problem adding a new one SISTEMA, for my cliente My Model looks like this:

public  class Cliente : Pessoa
{
 public virtual ICollection<TipoDeSistemas> TipoDeSistemas { get; set; }

 public void AdicionarSistema(TipoDeSistemas tipoDeSistemas)
   {
            TipoDeSistemas.Add(tipoDeSistemas);
   }
}
public class TipoDeSistemas
{
 public virtual ICollection<Cliente> Cliente { get; set; }
{

In my layer of service I do so:

public void adicionarSistema(ClienteViewModel cliente, List<Guid> tipoDeSistemasId)
{
  var c = _clienteRepositorio.BuscarPorId(cliente.ClienteId);
// c => é o cliente vindo do banco
 foreach (var sistema in tipoDeSistemasId)
 {
  var ts = _tipoDeSistemaRepositorio.BuscarPorId(sistema);
   c.AdicionarSistema(ts);
 }
_clienteRepositorio.Atualizar(c);
}

You are not adding a new system to the table that receives the n:m I don’t know if I did the mapping wrong. It’s like this:

 public class ClienteConfig : EntityTypeConfiguration<Cliente>
 {
         public ClienteConfig()
        {
         HasMany(c => c.TipoDeSistemas)
                .WithMany()
                .Map(m =>
                {
                    m.MapLeftKey("Cliente_ClienteId");
                    m.MapRightKey("TipoDeSistema_SitemaId");
                    m.ToTable("Sistema_Cliente");
                });
        }
 }

I changed the map, and I left it like this, to see if that’s it:

  public class ClienteConfig : EntityTypeConfiguration<Cliente>
    {
        public ClienteConfig()
        {
        HasMany(c => c.TipoDeSistemas)
                .WithMany(c => c.Cliente);
        }
    }

and did the same thing in the System configuration:

public class TipoDeSistemaConfig: EntityTypeConfiguration<TipoDeSistemas>
    {
        public TipoDeSistemaConfig()
        {

            HasMany(t => t.Cliente)
                .WithMany(t => t.TipoDeSistemas);
        }
    }

But still, it still doesn’t add the System(s) to the customer. I debugged the code, and checked, that in my repository is coming the added system Cliente.TipoDeSistema.Count(1)

the repository Atulizar esta Assim:

public virtual void Atualizar(TEntidade obj)
        {
            var entry = Db.Entry(obj);
            DbSet.Attach(obj);
            entry.State = EntityState.Modified;
            SaveChanges();
        }

it goes through this method normally, saved, and returns, everything that comes in OBJ updates, but it does not add sistema_cliente, in the table that receives the N:M No error, but tbm does not save the system(s) to the customer in the bank.

I tried using DAO, and theoretically, it’s working. I am inserting straight into the table that receives the N:M I don’t know if it’s good practice, but it momentarily solved the problem. since using Entity did not add.

Class Clientedao:

 public void Inserir(Cliente cliente, TipoDeSistemas tipoDeSistemas)
        {
            SqlCommand comando = new SqlCommand();

            comando.CommandType = CommandType.Text;
            comando.CommandText = "insert into ClienteTipoDeSistemas(Cliente_ClienteId, TipoDeSistemas_TipoDeSistemasId) values(@Cliente_ClienteId, @TipoDeSistemas_TipoDeSistemasId)";
            comando.Parameters.AddWithValue("@Cliente_ClienteId",cliente.ClienteId);
            comando.Parameters.AddWithValue("@TipoDeSistemas_TipoDeSistemasId", tipoDeSistemas.TipoDeSistemasId);

            Conexao con = new Conexao();
            con.Crud(comando);
        }
  • How come you don’t save and you don’t make a mistake? You didn’t miss a SaveChanges() on the way?

  • I edited the question, and put the method ATUALIZAR

  • I didn’t understand that line entry.State = EntityState.Modified;... at that point if the state is no longer as modified, it already has a problem. Something wrong with your Mapping or with your method AdicionarSistema()

  • Not that part, no, because I tried so tbm db.Entry(cs).State = EntityState.Modified;&#xA; db.SaveChanges(); and even then does not add the system in the client

  • In fact, I removed the responsibility of adding the method, and used the add right in the service: TipoDeSistemas ts = _tipoDeSistemaRepositorio.BuscarPorId(sistema);&#xA; cliente.TipoDeSistemas.Add(ts);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.