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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:12:53+00:00 2026-05-26T13:12:53+00:00

I start learn EF Fluent API. I have 2 simple POCO classes. public class

  • 0

I start learn EF Fluent API.

I have 2 simple POCO classes.

public class Customer
{
    public int CustomerId{ get; set;}
    public string Name{ get; set;}
}

public class Project
{
    public int ProjectId { get; set; }
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public Customer Customer { get; set; }
}

context class

   public class MyCtx:DbContext
    {
        public DbSet<Project> Projects { get; set; }
        public DbSet<Customer> Authors { get; set; }

        public MyCtx(string  connString):base(connString)
        {}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //PK
            modelBuilder.Entity<Project>()
                .HasKey(p => p.ProjectId)
                .Property(p => p.ProjectId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .HasColumnName("PROJECT_ID")
                .IsRequired();


            modelBuilder.Entity<Project>()
                .Property(c => c.Name)
                .HasColumnName("NAME")
                .IsRequired();

            //--------------------------------------------------------------------

            //PK
            modelBuilder.Entity<Customer>()
                .HasKey(c => c.CustomerId)
                .Property(c => c.CustomerId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .HasColumnName("CUSTOMER_ID")
                .IsRequired();

            modelBuilder.Entity<Customer>()
                .Property(c => c.Name)
                .HasColumnName("NAME")
                .IsRequired();

            base.OnModelCreating(modelBuilder);
        }
    }

I defined that CustomerId will be primary key in table customer and ProjectId will be primary key in project table.

I am little surprised with this behavarior. CustomerId in project table is automatically foreign key.

This behavior is based on naming convention? Or how it works?

  • 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-26T13:12:54+00:00Added an answer on May 26, 2026 at 1:12 pm

    Yes, it is based on a naming convention, in this case specifically the NavigationPropertyNameForeignKeyDiscoveryConvention:

    Convention to discover foreign key properties whose names are a
    combination of the dependent navigation property name (Customer in your case) and the
    principal type primary key property name(s) (CustomerId in your case).

    Or it is the PrimaryKeyNameForeignKeyDiscoveryConvention:

    Convention to discover foreign key properties whose names match the
    principal type primary key property name(s).

    I am not sure which one.

    If you don’t like one of those conventions you can remove them with the model builder:

    modelBuilder.Conventions
        .Remove<NavigationPropertyNameForeignKeyDiscoveryConvention>();
    // etc.
    

    A complete list of all conventions is here.

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

Sidebar

Related Questions

I have written the following simple test in trying to learn Castle Windsor's Fluent
I start to learn class in PHP. According to my experience with other language,
I have just start to learn wpf. Can anybody clear me the following to
I recently start to learn PHP. <?php echo (int) ( (0.1+0.7) * 10 );
I want to start and learn coding to create a web application. I have
I start to learn asm using fasm, unfortunately after compile code below I get
I start to learn AnyEvent and have some trobles with it. I totally misunderstood
I have recently start to learn WPF + MVVM. It is possible I have
Hi I start learn Fluent NHibernate. I am using this tutorial http://www.d80.co.uk/post/2011/02/20/Linq-to-NHibernate-Tutorial.aspx . Here
I'm a little bit confused. I want to start learn Flex3 with Eclipse and

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.