Answered by:
[ Entity Framework ] Insert new record with relationship one-to-many

Question
-
User-590094232 posted
This is my class
public class Product { public int ProductId { get; set; } public string Name { get; set; } public virtual Category Category { get; set; } }
How to insert this product, my problem is, I can't set category_id for this product
Thursday, September 12, 2013 8:43 AM
Answers
-
User697462465 posted
Hi giangnt,
I think your class need to update, please try to refer to the following code:
The Product.cs file:
public class Product { public int ProductId { get; set; } public string Name { get; set; } public virtual List<Category> Category { get; set; } }
The Insert method:
public void InsertData() { Product pro = new Product(); pro.ProductId = 1; pro.Name = "name1"; Category cat1 = new Category(); Category cat2 = new Category(); Category cat3 = new Category(); cat1.CategoryID = "cat01"; cat2.CategoryID = "cat02"; cat3.CategoryID = "cat03"; pro.Category.Add(cat1); pro.Category.Add(cat2); pro.Category.Add(cat3); using (DemonEntity ctx = new DemonEntity()) { ctx.InsertIntoProducts(pro); ctx.SaveChanges(); } }
Hope it can help you.
Best Regards,
Terry Guo- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 16, 2013 8:14 AM -
User441938976 posted
You can Initialize Object by below code :
Product p = new { ProductId="1", Name="ProductName", obj = new { CategoryId=1, CategoryName="XYZ" } };
If you want to more detail then you can refer below link :
http://newcome.wordpress.com/2010/06/11/c-object-literal-notation/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 16, 2013 8:58 AM
All replies
-
User-1219482859 posted
HI,
refer below article for your solution. its a quick and short tutorial on how to work with one-one or one to many relationships
http://www.entityframeworktutorial.net/add-one-to-one-entities-in-entity-framework.aspx
Friday, September 13, 2013 3:48 AM -
User697462465 posted
Hi giangnt,
I think your class need to update, please try to refer to the following code:
The Product.cs file:
public class Product { public int ProductId { get; set; } public string Name { get; set; } public virtual List<Category> Category { get; set; } }
The Insert method:
public void InsertData() { Product pro = new Product(); pro.ProductId = 1; pro.Name = "name1"; Category cat1 = new Category(); Category cat2 = new Category(); Category cat3 = new Category(); cat1.CategoryID = "cat01"; cat2.CategoryID = "cat02"; cat3.CategoryID = "cat03"; pro.Category.Add(cat1); pro.Category.Add(cat2); pro.Category.Add(cat3); using (DemonEntity ctx = new DemonEntity()) { ctx.InsertIntoProducts(pro); ctx.SaveChanges(); } }
Hope it can help you.
Best Regards,
Terry Guo- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 16, 2013 8:14 AM -
User441938976 posted
You can Initialize Object by below code :
Product p = new { ProductId="1", Name="ProductName", obj = new { CategoryId=1, CategoryName="XYZ" } };
If you want to more detail then you can refer below link :
http://newcome.wordpress.com/2010/06/11/c-object-literal-notation/
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 16, 2013 8:58 AM