Concurrency check fails with hierarchical data EF 4.0
I got a problem with Entity Framework 4.0
I have a hierarchical table Category: Id, Name, ParentCategory_Id, timestamp
The "timestamp" field is marked as "Concurrency Mode" 开发者_开发技巧= "Fixed"
And I'm using Self-Tracking Entity "Category" to manage Category entity in my MVC application.
The situation:
- I create STE "NewCategory",
- set field Name='bla-bla'
- create new STE "ParentCategory" like this:
var ParentCategory = new Category{Id=45}; ParentCategory.MarkAsUnchanged(); NewCategory.Parent = ParentCategory;
- Call ApplyChanges() method in my STE Context and call SaveChanges()
- The Query is "Update Category set Name=...." !!!!!!
If I do NewCategory.Parent = null
OR set "Concurrncy Mode" = "Node"
in model scheme everything works FINE.
How to use hierarchical data with concurrency check ?
I solved this problem. The solution is that the EF4.0 could manage theese situations only if you will use FK Assotiation properties.
In this sample the right way to do:
1)Create self assotiation in Category entity in edmx model. In "Referential Constraint" create link with parent entity via ParentCategory_Id property.
2) Create new instance of Category STE.
3) Set relation:
NewCategory.ParentCategory_Id = 45;
4) ApplyChanges, SaveChanges.
精彩评论