Hello SO,
I'm using LINQtoSQL to create cascading deletes IE: if Category is deleted then all the Products in that category are also deleted.
I have a Products repository set up already (IProductsRepository) and now I'm working in my Category repository to add the business domain logic for the cascade.
Products repository has this method :
public void DeleteProducts(IList<Product> Products)
{
foreach (Product product in Products)
{
productsTable.DeleteOnSubmit(product);
}
productsTable.Context.SubmitChanges();
}
Then I am creating a delete category method in my Category repository:
public void DeleteCategory(Category category)
{
IProductsRepository productsRepository;
var CascadeDeleteProducts =
from Products in productsRepository.Products
where Products.CategoryID == category.CategoryID
select Products;
productsRepository.DeleteProducts(CascadeDeleteProducts.ToList());
categoriesTable.DeleteOnSubmit(category);
categoriesTable.Context.SubmitChanges();
}
Visual Studio 2010 gives me an error in the above function for this line: from Products in productsRepository.Products
. It says
Use of unassigned local variable 'productsrepository'
What could be causing this error? I'm creating the products repository through DI with this line: IProductsRepository productsRepository;
. What am i doing wrong?
Edit
I neglected to mention that I am using Ninject to instatiate the product repository. So I beleieve that this line:IProductsRepository productsRepository;
is effectually declaring and initializing the product repository.