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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:25:31+00:00 2026-05-29T23:25:31+00:00

I am trying to set up a simple one-to-many relationship where a list can

  • 0

I am trying to set up a simple one-to-many relationship where a list can have zero or more tasks and a task must have a list associated. I want to assure this behavior with a unit test but I can’t figure out how the association has to be, so that the test will fail (later I will use an ExpectedExceptionAttribute, but I don’t even know which exception will be thrown yet).

The Task can be successfully submitted, but I would like to see Linq complain about a missing List reference:

[TestMethod]
public void SavingTaskWithoutListFails()
{
    var task = new Task() { Title = "Test Task", Description = "This is the task description." };
    Db.Tasks.InsertOnSubmit(task);
    Db.SubmitChanges();            
}

My classes and associations look like this:

[Table]
public class Task
{
    [Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
    public int TaskId { get; set; }

    [Column(IsPrimaryKey = true)]
    public string Title { get; set; }

    [Column]
    public string Description { get; set; }

    [Column]
    public int ListId { get; set; }

    private EntityRef<List> _list;

    [Association(Storage = "_list", ThisKey = "ListId")]
    public List List
    {
        get { return _list.Entity; }
        set { _list.Entity = value; }
    }
}

and this:

[Table]
public class List
{
    [Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
    public int ListId { get; set; }

    [Column(IsPrimaryKey = true)]
    public string Title { get; set; }

    private EntitySet<Task> _tasks;

    [Association(Storage = "_tasks", OtherKey = "TaskId")]
    public EntitySet<Task> Tasks
    {
        get { return _tasks; }
        set
        {
            if (_tasks == null)
            {
                _tasks = new EntitySet<Task>();
            }
            _tasks.Assign(value);
        }
    }
}

Can anyone help with the correct association or a hint how to achieve the desired behavior?

  • 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-29T23:25:34+00:00Added an answer on May 29, 2026 at 11:25 pm

    You should use [Column(CanBeNull = false)] for association columns to achieve that.

    Edit:

    [Table]
    public class Task
    {
        [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
        public int TaskId { get; set; }
    
        [Column]
        public string Title { get; set; }
    
        [Column]
        public string Description { get; set; }
    
        [Column(CanBeNull =  false)]
        public int ListId { get; set; }
    
        private EntityRef<List> _list;
    
        [Association(Storage = "_list", ThisKey = "ListId", IsForeignKey = true, OtherKey = "ListId")]
        public List List
        {
            get { return _list.Entity; }
            set { _list.Entity = value; }
        }
    }
    
    [Table]
    public class List
    {
        [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
        public int ListId { get; set; }
    
        [Column]
        public string Title { get; set; }
    
        private EntitySet<Task> _tasks;
    
        [Association(Storage = "_tasks", OtherKey = "TaskId", ThisKey = "ListId")]
        public EntitySet<Task> Tasks
        {
            get { return _tasks; }
            set
            {
                if (_tasks == null)
                {
                    _tasks = new EntitySet<Task>();
                }
                _tasks.Assign(value);
            }
        }
    }
    

    I moved IsPrimaryKey to Id instead of Title for both tables, set one side of an association as IsForeignKey=true and fill both ThisKey and OtherKey for both sides of association. When I tried to add new task without ListId value SubmitChanges throwed SlqCEException with following message: A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_Task_List ]

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

Sidebar

Related Questions

I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
I'm trying to define a simple one-to-many relationship between two poco's , using the
I have created a wpf vb.net project and am trying to set a simple
I'm trying to set up a simple Roles model for my site. Users can
OK, first my simple Domain Model is 2 classes with a one-to-many relationship, a
i have two simple models that have one-many relation public class X{ public int
I am trying to set up a simple TCP connection on a single port
I'm trying to set up a simple chat system with jQuery / Ajax ,
I am trying to set up a simple single-view iOS5 iPhone application (with AppDelegates,
I'm trying to set up a simple server side RSA encryption of a small

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.