Entity Framework; Object-Oriented delete
I'm building out a stock management system at the moment, using Entity Framework 4.
A little background, my entities go a bit like this (only showing needed info)
Product --> ProductLocations
WarehouseLocation --> has many ProductLocations
Each ProductLocation
has a Quantity
What I'm trying to do is have it so that when you call something like Product.TakeFromLocation(wl as WarehouseLocation, qty as Integer), it deletes the ProductLocation if its quantity falls to zero.
However... Product is an entity, as is ProductLocation, and they are meant to be persistance ignorant. I'm using the POCO EF templates, with a couple of modifications so that it produces an IEntities
interface, and generates a FakeEntities using in-memory versions for testing. This means my entities do not know anything about Entity Framework, and don't inherit from anything or implement any interfaces, so Context.Dele开发者_如何转开发teObject()
is out of bounds.
Anyone encountered a similar scenario and got any ideas on how to get round this?
I kind of thought that if SaveChanges() on the context is a partial method, I could extend it to check for 0-quantities - but then it's going to do this for all saves, which is a bit of a drag on 90%+ of operations that aren't doing this.
I would do it in the SavingChanges
event, not SaveChanges()
.
As for the performance "overhead," I suspect it's somewhere between trivial and un-measurable.
精彩评论