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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:30:21+00:00 2026-06-10T15:30:21+00:00

I’m trying to get Entity Framework to create the tables in a certain way

  • 0

I’m trying to get Entity Framework to create the tables in a certain way for a special 1-to-1 relation.

Let’s say I have this set of classes:

public class Parent
{
    public int ID { get; set; }
    public string Title { get; set; }
    public Child XYZ { get; set; }
}

public class Child
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
}

The XYZ property of the Parent class MAY be set to an instance of Child. I would like EF to create a table for each of the two classes, and the table for Child objects should simply have a foreign key column, ideally named “ParentID”, to keep the relation with the Parent object.

I do not want a “ChildID” column in the Parent table, and I do not want to add an ID or ParentID property to the Child class.

Currently, the properties of the Child object are created as columns in the Parent table, AND I get an exception if the XYZ property is not set to an instance of a Child class, i.e. it can’t be null.

I’m trying to do this with the DbModelBuilder and the OnModelCreating event. The IsOptional method only work with simple types.

Any suggestions? Is this even possible with EF?

  • 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-10T15:30:23+00:00Added an answer on June 10, 2026 at 3:30 pm

    Currently, the properties of the Child object are created as columns
    in the Parent table, AND I get an exception if the XYZ property is not
    set to an instance of a Child class, i.e. it can’t be null.

    Yes it cannot be null because currently your Child is a complex type (value object) and it must be always set (it is part of the entity and entity doesn’t exist without this part). If the content of the complex type is optional you must enforce this on Child’s properties not on Child itself.

    I do not want to add an ID or ParentID property to the Child class.

    If you want to have Child mapped to separate table it must have ID because every entity must have primary key. That ID will be also used as foreign key to Parent to achieve one-to-one relation:

    public class Child
    {
        public int Id { get; set; }
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }
    }
    

    And relation mapping:

    modelBuilder.Entity<Parent>()
                .HasOptional(p => p.XYZ)
                .WithRequired();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get Entity Framework Code First to create a database for me
I'm trying to use the Entity Framework to create a complex query and get
I am trying to create a completely dynamic way to query entity framework using
Trying to get my head around Entity Framework. I have a database with two
I am trying to get identifiying relationships to work in Entity Framework so I
I am using Entity Framework. I am trying to get the results from linq
I'm trying to create a base repository for use with Entity Framework 4.0 and
I'm trying to create a billing database with Entity Framework 4.3 using Code First
Im trying to get my head around the entity framework 4.1 codefirst approach for
I'm trying to use ASP MVC 4 and Entity Framework 4 to create pretty

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.