Again i’ve got a problem with visual studio lightswitch. Now its about custom validation. I’ve got an entity which has a foreign key referring to another entity:

RouteItem contains a foreign key to the FacilityItem which looks like this:

What i now want to do is to validate the ROUTENAME in the RouteItem. I have to make sure that the routname does not already exists in the selected Facility.
My validation code looks like this:
IEnumerable<RouteItem> items = this.DataWorkspace.dsMESOracle.dtRoutes.GetQuery().Execute();
if (this.Details.EntityState == EntityState.Added)
{
items = items.Where(e => e.ROUTENAME == ROUTENAME && e.MES_FACILITY == MES_FACILITY);
if (items.Count() > 0)
{
results.AddPropertyError(ValidationMsgRes.ROUTES_ROUTENAME_ALREADY_EXISTS);
}
}
else if (this.Details.EntityState == EntityState.Modified)
{
//Ignore the current entity if its modified otherwise it will always get count > 0
items = items.Where(e => e.ROUTENAME == ROUTENAME && e.MES_FACILITY == MES_FACILITY && e != this);
if (items.Count() > 0)
{
results.AddPropertyError(ValidationMsgRes.ROUTES_ROUTENAME_ALREADY_EXISTS);
}
}
So far the validation logic works perfect – it does what it should.
The main problem is that accessing the this.MES_FACILITY leads to the following error if lightswitch tries to save / update the changes:

the error also occurs if i ONLY write acilityItem temp = this.MES_FACILITY; in the validation implementation.
If i remove the all references / accesses what ever to this.MES_FACILITY in the validation implementation everything works perfect.
It also doesn’t matter if validation is true or false. The problem always occurs!
PS: The Problem also occurs with other entities and foreign keys its not a problem of the RouteItem – FacilityItem combination
Found a solution for this error / problem:
http://msdn.microsoft.com/en-us/library/bb738618.aspx
http://knowledgebase.progress.com/articles/Article/000028868
Workaround
1. Clean whole solution
2. Search the whole solution / project folder (in Explorer not in VisualStudio or by using Notepad++) for ConcurrencyMode=”Fixed” and remove it
3. Rebuild solution
4. Be happy!
It worked for me 🙂