A third step in my simple WCF Web service using Entity Framework
This is the next step in my previous sample (see the previous post from this month). Now I want to add and delete products using my Web service and EF.
The problem I found: I had to go back to the database and set a cascade delete for the FK_ProductInventory_Product (because I forgot to do it initially). Then I did an update of the EF model. The .edmx file was not quite updated... in the SSDL section I found the expected code:
<Association Name="FK_ProductInventory_Product">
<End Role="Product" Type="ProductionModel.Store.Product" Multiplicity="1">
<OnDelete Action="Cascade"> />
</End>
but on the CSDL section it was only:
< Association Name="FK_ProductInventory_Product">
<End Role="Product" Type="ProductionModel.Product" Multiplicity="1" >
<End Role="ProductInventory" Type="ProductionModel.ProductInventory" Multiplicity="*"/>
</Association>>
so I had to add manually the OnDelete part:
<Association Name="FK_ProductInventory_Product">
< End Role="Product" Type="ProductionModel.Product" Multiplicity="1" >
<OnDelete Action="Cascade"></OnDelete>
</End >
<End Role="ProductInventory" Type="ProductionModel.ProductInventory" Multiplicity="8"/>
</Association>
and now it's working fine. I used VS2008 (.NET3.5 SP1) on Windows Vista. I will ask the product team if it's a known bug...
Here is the code (on the server side) for adding one product, adding a list of products and delete a product
///
/// delete the product and all the childs
///
///
///
public int DeleteProduct(Guid prodId)
{
try
{
var product = (from prod in productionContext.Products.Include("ProductInventory")
where prod.ProductID == prodId
select prod).First();
if (product != null)
{
this.productionContext.DeleteObject(product);
return this.productionContext.SaveChanges();
}
return 0;
}
catch (InvalidOperationException ioex)
{
throw ioex;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// add a product with details
///
///
///
///
///
///
///
public int AddProduct(string name, decimal price, string shelf, string bin, string location, string city)
{
try
{
Product product = Product.CreateProduct(Guid.NewGuid(), name);
product.Price = price;
product.ModifiedDate = DateTime.Now;
product.IsNewFlag = true;
ProductInventory inventory = ProductInventory.CreateProductInventory(Guid.NewGuid());
inventory.Bin = bin;
inventory.Shelf = shelf;
inventory.Location = Location.CreateLocation(Guid.NewGuid());
inventory.Location.Description = location;
inventory.Location.City = city;
product.ProductInventory.Add(inventory);
this.productionContext.AddToProducts(product);
return this.productionContext.SaveChanges();
}
catch (InvalidOperationException ioex)
{
throw ioex;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// add a product list with children
///
///
public int AddProduct(List list)
{
try
{
if (list != null & list.Count > 0)
{
foreach (Product p in list)
{
this.productionContext.AddToProducts(p);
}
return this.productionContext.SaveChanges();
}
return 0;
}
catch (InvalidOperationException ioex)
{
throw ioex;
}
catch (Exception ex)
{
throw ex;
}
}
Comments
- Anonymous
May 31, 2009
PingBack from http://woodtvstand.info/story.php?id=11485