0

'm saving customers this way:

tb_clientes tb = new tb_clientes();
tb.clienteNOME = Cliente.clienteNOME;
tb.clienteCNPJ = Cliente.clienteCNPJ;
tb.clienteIE = Cliente.clienteIE;
tb.clienteENDERECO = Cliente.clienteENDERECO;
tb.clienteNUMERO = Cliente.clienteNUMERO;
tb.clienteCOMPLEMENTO = Cliente.clienteCOMPLEMENTO;
tb.clienteBAIRRO = Cliente.clienteBAIRRO;
tb.clienteCIDADE = Cliente.clienteCIDADE;
tb.clienteUF = Cliente.clienteUF;
tb.clienteOBS = Cliente.clienteOBS;
DB DB = new DB();

mydbEntities1 entidade = DB.contexto;
entidade.tb_clientes.Add(tb);
entidade.SaveChanges();

I have a contacts table, which will add contacts to a customer. I have to do a foreach in datagridview contacts and insert in the database. How can I do a foreach to save contacts?

mauriciosouza
  • 41
  • 1
  • 1
  • 5

1 Answers1

0

Not really sure if I understood you question. Hopefully this helps.
If you need to add multiple clients,

tb_clientes tb = new tb_clients();  
entidade.tb_clientes.Add(tb);  
tb = new tb_clients(); //create another  
entidade.tb_clientes.Add(tb); //add the 2nd one  
entidade.SaveChanges(); // you only need to call this once to save all changes.  

You also mentioned you had a customer and want to add clientes to customer. You'll have to set the customer otherwise your clientes will remain orphaned.

tb.Customer = yourCustomer;  

If you do the above, and assuming yourCustomer is already in the context, you will not need this line

entidade.tb_clientes.Add(tb);  

Since EF will know what you intended to do and automatically add tb to the context for you.

You can also add clientes directly to the customer

tb_clientes tb = new tb_clientes();
yourCustomer.Clients.Add(tb);
tb = new tb_clients(); //create another  
yourCustomer.Clients.Add(tb); //add the 2nd one  
entidade.SaveChanges();

Edits in response to OP's comments

From your images, it looks like you are creating a many-many relationship between the tables. Will your tb_clients_contatos table contain payload in the future? If not, the easiest thing would be to drop the contato_clientID column and make the remain 2 columns PK. If you update the EF at this point, it will create a many-many relationship automatically. You will not have to deal with this table since EF will maintain tb_clients_contatos for you transparently. Here's the new code

tb_clientes tb = new tb_clientes();
yourCustomer.Clients.Add(tb); //you're done at this point.  
    //A row in tb_clients_contatos will automatically be inserted when you SaveChanges.
tb.Customers.Add(yourCustomer); //this will work too.

If your relationship tables do need payloads, here's a good post on it. Create code first, many to many, with additional fields in association table

Community
  • 1
  • 1
user2880486
  • 1,068
  • 7
  • 16
  • I have to save one customers and many contacts for the same customer. Printscreens that follows will help you understand. http://s18.postimg.org/uf20ae8ix/foto1.jpg http://s18.postimg.org/bcn817mw9/foto2.jpg http://s18.postimg.org/fkdew7yy1/foto3.jpg – mauriciosouza Oct 15 '13 at 01:10
  • I think it'll be helpful if you can post your code as well. It's possible you don't even need the loop to save. Also your model shows many-many or at least 2 1-many relationships. But your post indicates you just need a 1-many. Changing the model might work better instead. – user2880486 Oct 18 '13 at 14:36