Already Generated the default classes. There are 2: 1. DataContext 2. Table object class
Now :
1. how to add functionality (extend) to the data class. i created seperate partial class.
-
do i need to do any extension to data context ?
-
Do i need to call generated-class constructor inside my own custom constructor ?
-
How to create new instance of the data class ?
A concern is that the DB name (Databahn) and (i dont know what else) is tightly attached by dbml. If i change my DB name etc. in future how does this pan out ?
And can i change the class names. Don’t want them to be same as DB table name ?
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="Databahn")]
public partial class AgentsDataContext : System.Data.Linq.DataContext
I generally hand roll my own datacontexts and data objects as well for Linq to Sql. I’ll create a sample datacontext, table object, and retriever method to show my little home made strategy here. We’ll say my database name is “Main”, my table object will be “dbo.Person” inside of sql.
here goes:
DataContext representing my databae:
Dto representing my table:
Now finally PersonRetriever class:
A few things to note: I declare a static mapping source in the datacontext to pass to the base data context, solely for keeping state when doing pre-compiled linq queries. It is not necessary, there is a base constructor that just takes in a connection string.
Also, make sure when declaring your Dtos, that the public property name matches EXACTLY to what your table in the database is. your private member can be named whatever you wish.
Hope this helps!