tags:

views:

148

answers:

2
+1  A: 

There are lots of potential solutions to this problem.

  • For strings: I use validators that use reflection to get the maximum lengths of the strings from the column attributes on the LINQ entity properties and check these. Alternatively you could handle the error that occurs on insert if the column would be truncated.

  • For dates: You could do a sanity check on the date (i.e., must be after some reasonable date) that must be entered by the user or for dates that can be automated, use a database generated default and mark the property as autogenerated and read-only in the designer. Don't put these dates on the form so they aren't set in the entity when you post the page. This works for "Create Date", etc. For modification dates, do something similar, but have the value generated by an update trigger instead of the default on update.

  • For booleans (which default to false): validate the value provider has an attempted value for the field in addition to doing validation on the entity itself. Alternatively, you could make the column nullable and check that it's not null. Both are compromises, but the latter makes the data model fit the validation framework so I prefer the former.

tvanfosson
+1  A: 

That's one way of doing things -- but it pretty much breaks the MVC pattern. The way you're performing the checks there is your basically allowing LINQ and the context to handle it -- which is why youre getting issues. Ideally, you want to create a layer between your controller and the actual data -- i.e. a Service layer for example (the business intelligence (BI) layer as it's called in teh business world).

In that service layer is where you would implement your rules -- such as the length of a name, the validity of a date, what is and isnt allowed. If there's something wrong there, then you can bubble up the errors and have the controller deal with them.

Ideally you want to create a level of abstraction between your controllers and your actual logic.

I'll see if i can get an example up shortly (Something's come up at the moment...)

MunkiPhD
IF we dont use LINQtoSQL class then What about MS Entity Framework?I don't know about this issue on MS E.F.... Let me check this
Vikas
Yes, Microsoft Entity Framework Does not have this issue about string, But for date as you suggested I can check it in My funtion called GetRuleViolation(). What you say about this? Right now I'm gonna complete my demo project. So should I Change my LINQtoSQL to Entity framework? It will be dangerous thing? Or can I enter a Logical layer as you said? If so then can you elaborate it more so it will help me create better baseline for MVC projects.
Vikas