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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:44:56+00:00 2026-05-30T13:44:56+00:00

Having odd problem with mapping tables with code-first EF. I have a parent class

  • 0

Having odd problem with mapping tables with code-first EF. I have a parent class “Contract” with a many-1 relationship from another class “Supplier”. The requirement came up to store a scanned copy of the contract within the Contract entity. To keep from having to query the document bytes every time, I want to store this property in a separate table. I can make EF store the ContractDocument in a separate table, but for some reason the FK to suppliers ends up in the ContractDocument table instead of the parent table.

public class Contract
{
    public Guid Id {get;set;}
    ...
    public virtual ContractDocument Document {get;set;}
    public virtual Supplier Supplier {get;set;}
}

public class ContractDocument
{
    public string MimeType {get;set;}
    public byte[] Data {get;set;}
}

In DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contract>()
                .Map(m => m.Properties(p => p.Document))
                .ToTable("ContractDocument");
    base.OnModelCreating(modelBuilder);
}

The table “ContractDocument” is created with correct properties, and does save data. However, it also contains the FK field “Supplier_Id”. The “Contract” table no longer contains any FK fields.

  • 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-30T13:44:58+00:00Added an answer on May 30, 2026 at 1:44 pm

    I’m not certain and don’t have VS to test at the moment, but I think when you split an entity into multiple tables, you need to map all the fields, not just the ones that are “unusual”.

    Try updating your mapping to something along these lines…

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Contract>()
                    .Map(m => {
                           m.Properties(p => 
                               new {
                                  p.Id,
                                  p.Document
                               });
                           m.ToTable("ContractDocument");
                        })
                    .Map(m => {
                           m.Properties(p =>
                               new {
                                  p.Id,
                                  /* everything else NOT document */
                               });
                           m.ToTable("Contract");
                        });
        base.OnModelCreating(modelBuilder);
    }
    

    Fixing based on this

    Edit
    based on your comment, it sounds like you need to define the relationship, not entity splitting. You have separate tables and separate classes, but the relationship between them is what is missing. Maybe try something like this.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Contract>().ToTable("Contract");
        modelBuilder.Entity<ContractDocument>().ToTable("ContractDocument");
        modelBuilder.Entity<Contract>()
            .HasOptional(c => c.Document)
            .WithRequiredDependent();
    

    2nd Edit
    Okay, to do it as a complex type, I think you need the first entity splitting map stuff plus a modelBuilder.ComplexType<ContractDocument>(); line.

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

Sidebar

Related Questions

I am having an odd problem with jQuery 1.5.2 in Chrome. I have code
I am having a really odd problem with Flash CS4. I have a MovieClip
We are having an odd problem with .NET Remoting. Basically, we have a server
I have a rather odd problem: I need to maintain a chunk of code
I'm having an odd problem with many-to-many relationships in EF CTP5 when a base
Having an odd problem printing an API-generated (V3) Google Map from Internet Explorer 7
I'm having a bit of an odd problem while trying to compile some code
I am having an odd problem with my SQL Server express 2005 DB. It
Can anyone help with an odd Ruby Gem problem I'm having - note I've
I'm having an odd probably with rails right now... a class is being defined

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.