Eric's Blog

Day to day experience in .NET
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

More LINQ to SQL Issues

I can't say that I'm impressed by LINQ to SQL, rather the oposite and I have bloged about problems before. Now it is time for more.

Bad Generated Code
The tables that have foregin key relationsships between them will result in objects, which have both an key-column (typical OtherObjectId) and a property that references the other object (typical OtherObject). There is also a partial method generated, typical called OnOtherObjectIdChanged. When writing code you will most often set the object property to the object that should be referenced and not set the Id-property. In my case I wanted to both set up a rule and set a default value for a property on the object when the referenced object is set. Easy I thought, and just used the partial method and implemented the code.

One problem though; The partial method is not called when the object property is set! In the generated code the local Id-variable is set, but it is not set through the property, which is the one that calls the partial method. I couldn't really se any reason to why they don't set it through the property, but even if there are some reason, I still belive that this is bad design. In the later case there should also be a OnOtherObjectChanged partial method that could be used. Instead I had to do a workaround using the property changed notification to check (with a string) if OtherObject property changed and in that case call tha partial method.

I will never use LINQ to SQL in any other project, and as soon as possible I will try to convert this project away from LINQ to SQL to Entity Framework or NHibernate. I actually belive that it is doubtly useful even for small projects after this year of more problems than value of LINQ to SQL.

Published den 29 januari 2009 21:23 by ericqu
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems