updating category id for the product instead of creating new id
i have two tables
product
product_id
product_Name
开发者_开发技巧 product_Price
product_Description
product_image
category_id
another table
category
category_id
category_name
category_description
i have a form with three text boxes(say tbProductPrice,tbProductName,tbProductdescription) one combo box(cbcategorytypes) two buttons one edit and another one is save button.. i am trying update the product table along with category_id
when i click the edit button the category names are loaded in the combobox
when we click on the save button what ever values in text boxes will be updated in product table along with category for that i have done the below code...
using (var vareditcontext = new abcEntities())
{
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
Image image = pictureBox1.Image;
byte[] bit = null;
bit = imageToByteArray(image);
product1 pd = vareditcontext.product1.Where(p => p.product_Id == productid
&& p.category_Id == productcategoryid).First();
string category = cbcategorytypes.Text;
var c = new category { category_Name = category }; //problem at this line
pd.category = c;
pd.product_Name = tbProductName.Text;
decimal price = Convert.ToDecimal(tbProductPrice.Text);
pd.product_Price = price;
pd.product_Description = tbProductdescription.Text;
pd.product_Image = bit;
vareditcontext.SaveChanges();
this.Close();
}
when i click the save button i got an exception like this ..
Argument out of range exception ..
I am getting this error because when i edit and trying to save the product details along with category name the new category name will be stored in database.....instead of updating the present one ...
how i can rectify this problem .. i mean not storing the new item i want to set the already exsiting category to the product...
is it possible with linq ....
would any one help on this..
many thanks....
I assume that category_id
is the Key
property for your category
entity. If you only load the category_name
into the combobox you actually need to load the Category
from the database because EF must know the key value when you assign the category to the product:
product1 pd = vareditcontext.product1
.Where(p => p.product_Id == productid
&& p.category_Id == productcategoryid)
.First();
category c = vareditcontext.categories
.Where(cat => cat.category_name == cbcategorytypes.Text)
.First(); // let's hope the name is unique to pick not the wrong one
pd.category = c;
// ...
vareditcontext.SaveChanges();
You could also only load the category_id and then leverage Dennis' approach to create a stub category and attach it to the context:
product1 pd = vareditcontext.product1
.Where(p => p.product_Id == productid
&& p.category_Id == productcategoryid)
.First();
int categoryid = vareditcontext.categories
.Where(cat => cat.category_name == cbcategorytypes.Text)
.Select(cat => cat.category_id)
.First();
var c = new category { category_id = categoryid };
// stub entity must have at least the key property set
vareditcontext.categories.Attach(c);
pd.category = c;
// ...
vareditcontext.SaveChanges();
If you would expose the category_id
column in the product
table as a foreign key property into your product
model class you could just set the category_id
:
product1 pd = vareditcontext.product1
.Where(p => p.product_Id == productid
&& p.category_Id == productcategoryid)
.First();
int categoryid = vareditcontext.categories
.Where(cat => cat.category_name == cbcategorytypes.Text)
.Select(cat => cat.category_id)
.First();
pd.category_id = categoryid;
// ...
vareditcontext.SaveChanges();
You need to either load the category from the context or attach the one you create on the fly to the context. Otherwise EF assumes you want to create and store a new one.
...
var c = new category { category_Name = category };
vareditcontext.Attach(c);
pd.category = c;
...
At MSDN, you can read more about Attaching and Detaching Objects.
精彩评论