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

  • SEARCH
  • Home
  • 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 6123059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:58:16+00:00 2026-05-23T15:58:16+00:00

This question is in two parts: A) Is my design wrong? If so, what

  • 0

This question is in two parts:

A) Is my design wrong? If so, what would be the proper way to do this?

B) If my design is right, how do I implement it?

I have a data class that consists of multiple instances of a component class. An abstraction of the class structure would be this:
Class Diagram

In this example I have only two components in the container but in reality I have more. It’s also important to point out that Container cares about which Component is which – they can’t just be a list, they have individual names and meaning to the Container.

I’ve implemented the following structure in MS SQL Server 2008 to store this data:

CREATE TABLE Containers (
  ID int IDENTITY(1,1) NOT NULL,
  ComponentA int NULL,
  ComponentB int NULL,
 CONSTRAINT PK_Containers PRIMARY KEY (ID));


CREATE TABLE Components (
  ID int IDENTITY(1,1) NOT NULL,
  Foo nchar(10) NOT NULL,
  Bar nchar(10) NOT NULL,
 CONSTRAINT PK_Components PRIMARY KEY (ID));


ALTER TABLE Containers
  ADD CONSTRAINT FK_ComponentA
  FOREIGN KEY (ComponentA)
  REFERENCES Components(ID);

ALTER TABLE Containers
  ADD CONSTRAINT FK_ComponentB
  FOREIGN KEY (ComponentB)
  REFERENCES Components(ID);   

This already has a few drawbacks:

  • In my model, components are unique, and can only belong to one property on one Container. In the DB, I have to enforce this manually.

  • When a Container is deleted, the Components it references should also be deleted. This I have to do manually in the DB (with a trigger).

The big problem, however, is this:

I use LINQ-TO-SQL for my database access in the C# WPF MVVM desktop application I am building in Visual Studio 2010. When I pull the database tables above into the LINQ-TO-SQL designer, this is what I get:

LINQ-TO-SQL Diagram

I can manually set the relationships to One-to-One in the LING-TO-SQL designer, to stop the Component instances from having sets of type Container.
Correctly, each Container has two Component references. However, the Component instances still have two Container references, where my model requires them to have only one.

How can I implement my class design in SQL Server or how can I convince LINQ-to-SQL to interpret my database the way I intended?

  • 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-23T15:58:17+00:00Added an answer on May 23, 2026 at 3:58 pm

    A container has many components, but needs to reference them by name. You need a relationship table from container to component (in pseudo-sql):

    create table ContainerComponents (
      ContainerID int FK references Container(ID),
      ComponentID int FK references Component(ID),
      Name string,
      -- optional - to give the relationship a meaning
      Type int FK references RelationshipType(ID)
    )
    

    To answer this question:

    In my model, components are unique, and can only belong to one property on one Container. In the DB, I have to enforce this manually.

    Just create a unique constraint in ContainerComponents for ContainerID and ComponentID.

    And this one:

    When a Container is deleted, the Components it references should also be deleted. This I have to do manually in the DB (with a trigger).

    Use cascade deletes from Container to ContainerComponents to Component.

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

Sidebar

Related Questions

This question has two parts (maybe one solution?): Sample vectors from a sparse matrix
This question is a bit of a two parter for .Net data services. This
This question has two parts. First, I need to isolate two strings of text
Of course I realize that there's no one right way to design a SQL
Two parts to this question 1) I am working with a group of six
This is a two part question. Is it possible to take advantage of xVal
This is a two-part question: First, I am interested to know what the best
This is a two-part question. I'm using jQuery for a project and wanting to
This is a two part question. The first is how do you properly close
This is a two part question, but I want to make sure the first

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.