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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:09:53+00:00 2026-06-06T03:09:53+00:00

I’m having trouble defining a foreign key relationship in entity framework. I’ve boiled the

  • 0

I’m having trouble defining a foreign key relationship in entity framework. I’ve boiled the code down to the minimum to demonstrate the problem.

The model is defined as ;

    public class AccessControlList
{
    [Key] public int Id { get; set; }
    public string Name { get; set; }
}

public class ManagedFile
{
    [Key] public int Id { get; set; }
    public string Name { get; set; }
    public virtual AccessControlList AccessControl { get; set; }
}

public class ManagedFolder
{
    [Key] public int Id { get; set; }
    public string Name { get; set; }
    public virtual AccessControlList AccessControl { get; set; }
    public virtual ISet<ManagedFile> Files { get; private set; }

    public ManagedFolder()
    {
        Files = new HashSet<ManagedFile>();
    }
}

So essentially, I have a single AccessControl entity that can be referenced by either a ManagedFile or ManagedFolder entity.

To make sure the foreign key is added to ManagedFile and ManagedFolder, I’ve added the following fluent mapping

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<ManagedFile>()
                    .HasRequired(mf => mf.AccessControl)
                    .WithRequiredDependent()
                    .Map(m => m.MapKey("AccessControlListId"));


        modelBuilder.Entity<ManagedFolder>()
                    .HasRequired(mf => mf.AccessControl)
                    .WithRequiredDependent()
                    .Map(m => m.MapKey("AccessControlListId"));

        base.OnModelCreating(modelBuilder);
    }

This builds a database that looks exactly as I expect, with a foreign key AccessControlListId on each of ManagedFile and ManagedFolder.

Entity Framework Created Schema

It all looks good to me, but when I try to insert some data, with this code

using(var ctx = new MyContext())
{
    var mf = ctx.Folders.Add(new ManagedFolder {Name = "$/"});
    mf.AccessControl = new AccessControlList();

    var file = new ManagedFile {Name = "myfile.txt"};
    mf.Files.Add(file);
    file.AccessControl = new AccessControlList();

    ctx.SaveChanges();
}

It throws an error…

Error

I can’t figure out what I’ve done that’s incorrect?

As I understand my mapping, EF can save a new AccessControlList entity, then use the Id to write either the ManagedFolder or ManagedFile table.

As always, I appreciate any help offered.

Thanks Andy

  • 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-06T03:09:55+00:00Added an answer on June 6, 2026 at 3:09 am

    Why does the database look “exactly as you expect”? Apparently you are trying to define two one-to-one relationships (.HasRequired(...).WithRequiredDependent(...)) but your relationships in the database are one-to-many.

    Do you want one-to-one to one-to-many relationships? In the latter case (which is easier) just replace the two WithRequiredDependent() methods by WithMany().

    If you want one-to-one you can’t define a separate foreign key AccessControlListId. You must use shared primary keys, i.e. the PK of the dependent is the FK at the same time. The mapping would have to be:

    modelBuilder.Entity<ManagedFile>()
                .HasRequired(mf => mf.AccessControl)
                .WithRequiredDependent();
    // the same for ManagedFolder
    

    …without the MapKey.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.