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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:43:00+00:00 2026-05-15T10:43:00+00:00

I would like to perform LINQ to SQL mapping in C#, in a many-to-many

  • 0

I would like to perform LINQ to SQL mapping in C#, in a many-to-many relationship, but where data is not mandatory.
To be clear:
I have a news site/blog, and there’s a table called Posts. A blog can relate to many categories at once, so there is a table called CategoriesPosts that links with foreign keys with the Posts table and with Categories table. I’ve made each table with an identity primary key, an id field in each one, if it matters in this case.
In C# I defined a class for each table, defined each field as explicitly as possible. The Post class, as well as Category class, have a EntitySet to link to CategoryPost objects, and CategoryPost class has 2 EntityRef members to link to 2 objects of each other type.

The problem is that a Post may relate or not to any category, as well as a category may have posts in it or not. I didn’t find a way to make an EntitySet<CategoryPost?> or something like that.

So when I added the first post, all went well with not a single SQL statement. Also, this post was present in the output. When I tried to add the second post I got an exception, Object reference not set to an instance of an object, regarding to the CategoryPost member.

Post:

[Table(Name="tm_posts")]
public class Post : IDataErrorInfo
{
    public Post()
    {
        //Initialization of NOT NULL fields with their default values
    }

    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
    public int ID { get; set; }

    private EntitySet<CategoryPost> _categoryRef = new EntitySet<CategoryPost>();
    [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_categoryRef", ThisKey = "ID", OtherKey = "PostID")]
    public EntitySet<CategoryPost> CategoryRef
    {
        get { return _categoryRef; }
        set { _categoryRef.Assign(value); }
    }
}

CategoryPost

[Table(Name = "tm_rel_categories_posts")]
public class CategoryPost
{
    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
    public int ID { get; set; }

    [Column(Name = "fk_post", DbType = "int", CanBeNull = false)]
    public int PostID { get; set; }

    [Column(Name = "fk_category", DbType = "int", CanBeNull = false)]
    public int CategoryID { get; set; }

    private EntityRef<Post> _post = new EntityRef<Post>();
    [Association(Name = "tm_rel_categories_posts_fk2", IsForeignKey = true, Storage = "_post", ThisKey = "PostID", OtherKey = "ID")]
    public Post Post
    {
        get { return _post.Entity; }
        set { _post.Entity = value; }
    }

    private EntityRef<Category> _category = new EntityRef<Category>();
    [Association(Name = "tm_rel_categories_posts_fk", IsForeignKey = true, Storage = "_category", ThisKey = "CategoryID", OtherKey = "ID")]
    public Category Category
    {
        get { return _category.Entity; }
        set { _category.Entity = value; }
    }
}

Category

[Table(Name="tm_categories")]
public class Category
{
    [Column(Name = "id", DbType = "int", CanBeNull = false, IsDbGenerated = true, IsPrimaryKey = true)]
    public int ID { get; set; }

    [Column(Name = "fk_parent", DbType = "int", CanBeNull = true)]
    public int ParentID { get; set; }

    private EntityRef<Category> _parent = new EntityRef<Category>();
    [Association(Name = "tm_posts_fk2", IsForeignKey = true, Storage = "_parent", ThisKey = "ParentID", OtherKey = "ID")]
    public Category Parent
    {
        get { return _parent.Entity; }
        set { _parent.Entity = value; }
    }

    [Column(Name = "name", DbType = "varchar(100)", CanBeNull = false)]
    public string Name { get; set; }
}

So what am I doing wrong? How to make it possible to insert a post that doesn’t belong to any category? How to insert categories with no posts?

  • 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-15T10:43:01+00:00Added an answer on May 15, 2026 at 10:43 am

    It seems that the error has nothing to do with mapping. Mapping is correct.
    As I wrote, the first post got inserted without problems, and the rest failed to insert. After deleting it from the database, I still couldn’t add posts. It became clear that it had nothing to do with either I had something in the DB or not, and only with the fact that I’ve made some changes to the code.

    So what are the changes? In Apress “ASP.NET MVC Pro”, the first example illustrated a way to validate data in an iterative way (non-declarative, using the facilities provided by IDataErrorInfo), to which I stuck. I done everything by that example, and the function call that should have validated the input screwed up my data flow, and threw that exception upon submitting to the database.

    Removed that validation, and everything worked fine.

    Sorry for the false alarms.

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

Sidebar

Related Questions

When doing linq-to-sql in c#, you could do something like this: var data =
I would like to perform a key event detection in textbox. The keys should
I would like to perform something similar to this (ie get the sum of
I would like to perform a bitwise exclusive or of two strings in python,
I would like to perform a measurement and plot a graph while the measurement
I would like to perform 5 consecutive reads to a slave devices and check
I would like to perform a certain selection which (as I tested in sqldeveloper)
In my plugin code I would like to perform a WP_Query (or similar) which
I'm working on a block module and would like to perform ajax operations on
I am new to Java, the function I would like to perform is to

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.