I don’t know about nhibernate but see if it helps you:
a tip would be you review the relationship of your tables, I see no need to have a foreign key for quantity and another for product name to purchase. You can have just the Id
Stock product in table compra
, Then when you made a purchase would only consult on estoque
by the id of the product purchased.
another thing:
I noticed that your estoqueDAO
does not have a data update method similar to public void Update(Compra compra)
purchase. Soon you can not update data from stock table, just create new ones with public void Adiciona(Estoque estoque)
.
create a method to update the data similar to the class compraDAo
in estoqueDao
, ai you can do the following, during your purchase logic:
/*Aqui voce pega uma lista de todos produtos*/
var listaDeProdutos = estoqueDao.Lista();
/*Aqui voce pega apenas o produto que está sendo tratado em compra*/
var produto = listaDeProdutos.Where(p => p.Produto == compra.NomeProduto);
/*Aqui voce altera a quantidade*/
produto.Quantidade -= compra.Quant;
/*Aqui atualiza o registro no banco com a nova quantidade*/
estoqueDao.Update(produto);
I tried to do based on what you posted, the ideal would be a method in EstoqueDao
looking for the product id, you wouldn’t need the Where
. I hope it helps you.
what is wrong?
– Cassio Alves
have little information there, you want to update the table
estoque
? and return the amount of products purchased to the form that is loaded in your action? if so, use theReturn View()
that is commented to return theViewModel
with the fields updated to theView
something likereturn View(compra)
, and in theView
userazor
to display, something like@Html.DisplayFor()
, and before that consult the product inestoque
forNomeProdutoId
which I imagine is a foreign key to the product in stock, change the quantity by subtracting the quantity purchased and save.– Vinicius
I intend when I make the purchase of a specific product, it make the change in the quantity of the stock, where the quantity of the purchase table, go to the stock table only for that stock product, an example would be - if I bought 10 pencils and registered in the purchase form this amount of 10 pencils already goes into the stock records, without I have to change automatically
– Guilherme Padovam
and what is your difficulty in doing this? could put the prints of the models.
– Cassio Alves
What I want to do is this, I don’t have a very broad understanding of c#, I only know the basics and what I need to do is add the purchase amount in stock, as if I automatically do an update adding.
– Guilherme Padovam
then you want to update the table
estoque
, you can already query and save data in the bank? thisdao.Adiciona(compra);
is adding the purchase in the BD table?– Vinicius
@Vinicius exactly, the complete crud I can do but I wanted when I registered a quantity in the table to buy the same value to be for the stock table
– Guilherme Padovam
I’m not sure if the relationship between the purchase and stock tables is correct, you can consult the product that had the quantity changed in the table
estoque
? if yes, I see that can be the way to make the product query change the quantity and save (update) the registration in the BD, inside the purchase controller?– Vinicius
@Vinicius can not, to make the relationship right, I would have to pull the purchase to stock? And if so, if so, I wanted to kind of like when I was going to set a value for a product that he registers on that product and not create a duplicate
– Guilherme Padovam
Only one addendum, you don’t need Else since the if block has a Return.
– Leandro Amarilha
Are you following any tutorial? can you write in the bank? consult something in the bank, save something in the bank and etc? as you said above you can already do CRUD, so if you refer(select) in the table
estoque
for the desired product, and then update(Update) the same one will not create another, but update the fields in the database, assuming that you can already make a query in the database– Vinicius
@Vinicius yes I can do the select and then change it, the point is that I want to do it from another form, that when registering the data in a table that would be the Purchase, change the data of the stock table, the problem is that it does not change
– Guilherme Padovam
that
dao
you use to write on the tablecompra
right? would you be able to put all the controller code? including how do you declare thisdao
?– Vinicius
@Vinicius I can for yes
– Guilherme Padovam