I have many tables with identical schemas but different names. Can I create a single representative DBML/DataContext and re-use it for all the tables, or do I have to drag every table into the dbml designer?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is possible to do this without dragging every table from the designer, BUT it’s a PITA because you have to manually define all the table mappings for each subsequent object (which is what the DBML designer is doing for you, in essence). Here’s an example:
Define a pair of tables:
Add some data to them:
Create a DBML file called TestsDb, and drag the Person table onto the surface. Now create the following partial class:
For convenience, I used ReSharper to extract an interface from Person, like so:
And then I created a new class called OtherPerson which implements that interface:
And test it all in a main function like so:
And this is the end result:
As you can see, it’s possible, but is it really worth it? I suppose it might be worth it if you had some sort of automatic code generation going on (as opposed to manual code generation triggered by editing the DBML file), but otherwise, probably not.
You could use the Entity framework though. I’m not too familiar with it, but I think it’s supposed to handle this sort of thing quite nicely, and EF4 is out now too.