C# – Add “extra data” to LINQ to SQL generated entity

clinq-to-sqlnet

I have a LINQ to SQL entity called Job which is just a simple table in SQL. Most of the time it works for what I need but occasionally I need more fields to be populated in this entity. I know I can use partial classes to add new fields to the LINQ generated class, but my question is how do I populate this extra data? For example, I have a stored proc that pulls back all the values for the Job table and the extra data I need to populate. I'd like to hook into the entity populate routines and when this data is present, populate it, when it's not, ignore it. I know the simple solution is to create a view, but I don't like that solution.

I can't seem to figure out where LINQ is taking the data and populating the entity. Does anyone have any ideas?

Best Answer

(wild guess) you may be able to create an entity from a SQL view. create the view in the db with the joins etc, like in the SP, then add that view to the LINQ designer and it should create an entity with all the gear u need.

OR

(better) Create a class that represents your complete entity (with optional fields) and map to the many entites (you presently have) as another step in the process. This longer coz you will prob want to create another layer of abstraction to do this.. and then to take it further you will prob want to abstract your whole entity set, etc etc.. So its more work.. but it brings you closer to a cleaner Domain Model (if that's what you want).