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 7574585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:28:17+00:00 2026-05-30T16:28:17+00:00

I have two classes. I want to map Class1 to a database table with

  • 0

I have two classes. I want to map Class1 to a database table with linq to sql.

[Table(Name = "SomeTable")]
public class Class1
{
    public Class2 Class2
    {
        get; 
        set;
    }
}    

class Class2
{
    public string Name1
    {
        get;
        set;
    }

    public string Name2
    {
        get;
        set;
    }

    public string Name3
    {
        get;
        set;
    }
}

Database table has 3 columns corresponding to the Name-properties of Class2. In NHibernate f.ex., I would use a ‘component’-element in the xml-mapping, but how to do with linq to sql?

  • 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-30T16:28:18+00:00Added an answer on May 30, 2026 at 4:28 pm

    As far as I know, LINQ to SQL doesn’t allow this. But as almost always there are several workarounds for it.

    The first one is to create a column-per-complex type property. See Complex Types in LINQ-to-SQL—Reloaded article for more details.

    The second one is to associate class Class2 with a table and add a reference to it in Class1 (primary/foreign key).

    And the last one is to store Class2 property of a type Class2 serialized. In the SomeTable it could be a column of type nvarchar(MAX) if you prefer JavaScriptSerializer or e.g. xml if you prefer serialization to XML.

    Your complex type is pretty simple, so I show an example of how to do it by using JavaScriptSerializer. We should add a fake property of type Class2, and a private property of type string which will actually store the serialized value:

    public partial class Class1
    {
        // this is autogenerated property
        private string Class2Internal {...}
    
        // this is a fake property
        public Class2 Class2 {get; set;}
    
        partial void OnLoaded()
        {
            var serializer = new JavaScriptSerializer();
            this.Class2 = serializer.Deserialize<Class2>(this.Class2Internal);
        }
    
        partial void OnValidate(ChangeAction action)
        {
            var serializer = new JavaScriptSerializer();
            this.Class2Internal = serializer.Serialize(this.Class2);
        }
    }
    

    The OnLoaded is called after data is loaded by LINQ to SQL, and OnValidate is called before the code is saved – it’s the best place for serializing/deserializing (note, that serialization occurs only on saving, and deserialization on entity load).

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

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
I have two classes: public class TestClass1 { public int TestInt { get; set;
So I have this two classes: public class PhysicalTest { public int ID {
I have two tables. With Sqlalchemy, I map them to two classes: class A(base):
I have two classes. First class has TabPage control. I want to change layout
I have two classes and want to have a reference from class Kunde to
I have two classes : class A { public function load() { $var =
First I want to describe my situation briefly. I have two classes, one MainClass
Given two model classes, Foo and Bar , I want Foo to have 3
I have two classes, Foo and Bar, that have constructors like this: class Foo

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.