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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:41:27+00:00 2026-06-14T10:41:27+00:00

Simple many to many relationship Here all the cases that I am trying to

  • 0

Simple many to many relationship
enter image description here

Here all the cases that I am trying to accomplish

  • case number 1. Both the file and the category are new
  • Case number 2. The file is not new and category is not new.
  • Case number 3. The file is new but the category is not new
  • Case number 4. The file is new but the categirt is not new.

Case Number 1 solution: everything works fine here

    using (MyContext DbCtx = new MyContext())
    {

        var FileCategory = new FileCategory { 
            Active=true, 
            Category="W",  
            File = new List<File>()  
        };
        var File = new File
        {
            FileName = "a",
            FileTypeId = 1,
            RevisionDate = DateTime.Now,
            UploadDate = DateTime.Now,
            FileCategory = new List<FileCategory>()
        };
        DbCtx.FileCategory.Add(FileCategory);
        FileCategory.File.Add(File);
        DbCtx.SaveChanges(); 
    }

Case Number 2: I get an error:NullReferenceException (

I think FileCategory navigational property is null from the MyExistingFile. Not sure is lazy loading is getting the best of me here.

  using (MyContext DbCtx = new MyContext())
            {
                var MyExistingFile = DbCtx.File.Find(1);
                var MyExistingCategory = DbCtx.FileCategory.Find(1);
                //with the line below i am trying to say we dint change anything on File since it already exist
                DbCtx.Entry<File>(MyExistingFile).State = EntityState.Unchanged;
                //I am just trying to add the category to a file. Since a file can multiple categories.
                MyExistingFile.FileCategory.Add(MyExistingCategory);
                DbCtx.SaveChanges(); 
            }

I am guessing I will be able to solve the rest once someone shows me why case number 2 is wrong.

Update: Solution was given by Slauma

I added virtual to all my poco properties and navigational properties like this

 public class FileCategory
    {
        [Key]
        public virtual int FileCategoryId { get; set; }
        public virtual string Category { get; set; }
        public virtual bool Active { get; set; }    
        public virtual ICollection<File> File { get; set; }
    }

Then I was able to write this and it worked for case number 2

        using (MyContext DbCtx = new MyContext())
        {
            var MyExistingFile = DbCtx.File.Find(1);
            var MyExistingCategory = DbCtx.FileCategory.Find(1);
            MyExistingFile.FileCategory.Add(MyExistingCategory);
            DbCtx.SaveChanges(); 
        }
  • 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-06-14T10:41:29+00:00Added an answer on June 14, 2026 at 10:41 am

    Just create an empty list:

    using (MyContext DbCtx = new MyContext())
    {
        var MyExistingFile = DbCtx.File.Find(1);
        var MyExistingCategory = DbCtx.FileCategory.Find(1);
        MyExistingFile.FileCategory = new List<FileCategory>();
        MyExistingFile.FileCategory.Add(MyExistingCategory);
        DbCtx.SaveChanges(); 
    }
    

    It’s actually better to avoid lazy loading in this scenario because it would load the FileCategory collection from the database. But you don’t need this collection to create the new relationship.

    Manually setting the state to Unchanged is not necessary, so I removed that line.

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

Sidebar

Related Questions

I'm trying this simple uni-directional one-to-one relationship Person ----> Address using many-to-one mapping with
I have a simple one-to-many relationship. I would like to select rows from the
Simple task: given that an article has many comments, be able to display in
I have a simple SQL relational model with a many to many relation. Here
I've been working for many hours trying to do a simple thing: use a
There are many solutions for third-party decentralized authentication that are pretty simple to set
I got a pretty simple parent/child relationship here, which looks like this: Email servers
General Case How do you perform a left join across a many-to-many relationship when
I have 2 entities, A and B that have a many to many relationship.
I'm trying to build a modified role system that has the following class/relationship structure:

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.