Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3672028
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:33:33+00:00 2026-05-19T02:33:33+00:00

Already Generated the default classes. There are 2: 1. DataContext 2. Table object class

  • 0

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.

  1. do i need to do any extension to data context ?

  2. Do i need to call generated-class constructor inside my own custom constructor ?

  3. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:33:33+00:00Added an answer on May 19, 2026 at 2:33 am

    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:

    internal class MainDataContext : System.Data.Linq.DataContext
    {
         private static MappingSource _mappingSource = new AttributeMappingSource();
    
         internal MainDataContext(string connectionString)
              : base(connectionString, _mappingSource) {}
    
         internal Table<PersonDto> PersonDtos { get { return GetTable<PersonDto>(); } }
    }
    

    Dto representing my table:

    [Table(Name = "dbo.Person")]
    internal class PersonDto
    {
        private Guid _id;
        [Column(Storage = "_id", IsPrimaryKey = true)]
        public Guid Id
        {
            get { return _id; }
            set { _id = value; }
        }
    
        private string _name;
        [Column(Storage = "_name")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    
        private DateTime _dateCreated;
        [Column(Storage = "_dateCreated")]
        public DateTime DateCreated
        {
            get { return _dateCreated; }
            set { _dateCreated = value; }
        }
    }
    

    Now finally PersonRetriever class:

    public class PersonRepository
    {
        private string _connectionString;
    
        public PersonRepository(string connectionString)
        {
            _connectionString = connectionString;
        }
    
        string GetPersonName(Guid personId)
        {
            using (var db = new MainDataContext(_connectionString))
            {
                string personName = string.Empty;
    
                PersonDto person = db.PersonDtos.FirstOrDefault(c => c.Id == personId);
    
                if (person != null)
                {
                    personName = person.Name;
                }
    
                return personName;
            }
        }
    }
    

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to reorder an already generated PDF file programmatically, and using as
I already have a User table in my primary application database with an email
I know there are a lot of threads here already on the repository pattern
Is there a way to test if a collection is already initialized? try-catch only?
Already implemented performance boosters : - Get compatible image of GraphicsConfiguration to draw on
I already know how to: Load properties files into my Spring configuration using: <context:property-placeholder
I already know the obvious answer to this question: just download <insert favorite windows
As already discussed in Lua as a general-purpose scripting language? Lua currently probably isn't
I already know Java, Objective C, C#, and some Python. I want to be
I already have a deploy.rb that can deploy my app on my production server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.