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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:56:31+00:00 2026-05-27T00:56:31+00:00

I want to create the following relationship. Obviously this is painfully basic but I’m

  • 0

I want to create the following relationship. Obviously this is painfully basic but I’m falling at the first hurdle thanks to EF. In my database SubForm‘s primary key, Id, is a foreign key referencing Application.Id (enforcing the relationships). When I add a new Application EF complains that I don’t have a SubForm_Id row, I don’t want this as this would be obviously be a duplicate of the primary key. Can I add some annotations to the model to sort this out?

This brings me to another issue, if I want to add a new Application with a new SubForm to the database at the same time – how could I get the Application Id (it is defined in the db as an auto-increment int) to set it on the SubForm object, or can EF do this for me?

Help would be appreciated.

  • 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-27T00:56:32+00:00Added an answer on May 27, 2026 at 12:56 am

    EF by convention assumes that your Application table has a scalar property SubForm_Id when you have a navigational property of type SubForm. You need to explicitly configure the Shared Primary Key mapping.

    public class MyContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
             modelBuilder.Entity<Application>()
               .HasOptional(a => a.SubForm)
               .WithRequired(s => s.Application);
        }
     }
    

    You can insert both as follows

    var app = new Application();
    var subForm = new SubForm { Application = app };
    db.SubForms.Add(subForm);
    db.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create the following element: <exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd"> If I use something
I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as 'SumAmount', SUM(sa.Cost)
I want to create URL re-write rules for following URL in htaccess. http://example.com/vedios/category/fun/ ->
I want to create a view from the following two tables: Entry: entry_id entry_date
I want to create a C++ class with the following type: It can be
I want to create an allocator which provides memory with the following attributes: cannot
I want to create an XSD which can generate the following XML. <note> <email:to>abc@def.com</email:to>
I want to create an alias for a class name. The following syntax would
I want to create classes Car, Vehicle, and Airplane with the following properties: Car
I have the following scenario, I want to create a DataGrid and then populate

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.