I have a product and category entities, and it has many to many relationship. So while creating the product, I will pass the list of categories in which product get mapped to it. But while creating the product , product alone creating and mapping is not
creating. below is the example.
var container = new Service.Container(new Uri(serviceBusUri));
Service.Products objProducts = new Service.Products();
objProducts.ProductName = "nokias";
objProducts.CreatedOn = DateTime.Now;
objProducts.CreatedBy = "suresh";
Service.Category objCategory=new Servi.Category();
objCategory= container.Categories.Where(c => c.ID == 36299).SingleOrDefault();
objProducts.Categories.Add(objCategory);
container.AddToProducts(objProducts); var response = container.SaveChanges();
Can anyone give me idea why product is not mapping to the category?
For this situation, it seems that the issue is more realted to the Entity Framework, however, this forum is focus on the web api issue. And you could post the issue to the Entity Framework Forum for further help:
While working separately with the entity framework, the above scenario is working fine without any issues. but while using it with web api, its not working.
from client am assigning the value to the collection property of entity like below.
in the odata service post method, while checking the entity collection, its coming as null.. ie if you check the product.categories in the below method.
public IHttpActionResult Post(Products product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Products.Add(product);
db.SaveChanges();
return Created(product);
}
I feel like it seems to be an issue with web api on passing the inputs in the post request and its not related to entity framework.
Member
43 Points
311 Posts
Create entity with navigation property
Nov 12, 2014 05:51 AM|ksureshh_pk|LINK
I have a product and category entities, and it has many to many relationship. So while creating the product, I will pass the list of categories in which product get mapped to it. But while creating the product , product alone creating and mapping is not creating. below is the example.
Can anyone give me idea why product is not mapping to the category?
Suresh.
All-Star
27133 Points
2676 Posts
Re: Create entity with navigation property
Nov 13, 2014 02:40 AM|Shawn - MSFT|LINK
Hi,
For this situation, it seems that the issue is more realted to the Entity Framework, however, this forum is focus on the web api issue. And you could post the issue to the Entity Framework Forum for further help:
http://forums.asp.net/1227.aspx/1?ADO+NET+Entity+Framework+LINQ+to+SQL+NHibernate
Regards
Member
43 Points
311 Posts
Re: Create entity with navigation property
Nov 16, 2014 11:31 PM|ksureshh_pk|LINK
Hi Shawn,
While working separately with the entity framework, the above scenario is working fine without any issues. but while using it with web api, its not working.
from client am assigning the value to the collection property of entity like below.
Service.Category objCategory=new Servi.Category();
objCategory= container.Categories.Where(c => c.ID == 36299).SingleOrDefault();
objProducts.Categories.Add(objCategory);
in the odata service post method, while checking the entity collection, its coming as null.. ie if you check the product.categories in the below method.
I feel like it seems to be an issue with web api on passing the inputs in the post request and its not related to entity framework.
Please correct me if am wrong.
Suresh.
None
0 Points
2 Posts
Re: Create entity with navigation property
Jun 01, 2015 01:07 PM|dtotzke|LINK
Hello Suresh,
Did you ever find a resolution to this problem? I am having the same issue with WebApi 2.2 and OData v4 with EF6. Very frustrating.
OData webapi2 EntityFramework6