views:

22

answers:

2

Suppose I have written a class Customer which has it attributes as Name, Age, designation etc. And I have a table also as customer with the same attributes.

Now I dont want to use the designer provided that generates the entity classes for me.

Is it possible for me to map the Customer.cs to the table customer while using LINQ to SQL as an OR/M.

+2  A: 

You're going to have a hard time doing this because your class won't have all the internal plumbing a L2S class has to manage FK relationships, changes, attachments to a data source etc.

OTOH, we have done a similar thing. In our L2S application, we have what we call "Application entities" which are our own entities (and much more lightweight), and we have the L2S generated entities (with all the internal L2S plumbing). In our back-end, the application entities map to a corresponding L2S entity for data manipulation. Works very well for us.

Randy

Randy Minder
A: 

Yes it is actually. You can provide a mapping file when you instantiante a DataContext like so:

var dataContext = new DataContext("connectionstring", entity.map);

This file can be autogenerated with SqlMetal.exe:

sqlmetal /server:servername /database:databasename /context:nameofdatacontext /Map:Entity.Map /code:Entity.code

So if you stick with the convention to name the properties the same in db and i enties, you will be up an running.

Furthermore you can use T4 templates to generate your datacontext, by also generating the dbml file.

I have created a .bat file, that i execute from solution explorer with powershell.exe, that

  • runs sqlmetal.exe with map parameter
  • runs sqlmetal.exe with dbml parameter
  • and runs TextTransform.exe on my T4 template that generates my datacontex.

But you should probably use EntitySet<T> and EntityRef<T> in your entity if you don't want lack normal functionality like lazyload. This obviously means that you will not have clean POCOs, but if you can live with that, its a fairly small tradeoff.

Luhmann