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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:49:00+00:00 2026-06-15T07:49:00+00:00

Entity Framework Code First: Suppose I want to create table relationships, where is the

  • 0

Entity Framework Code First:

Suppose I want to create table relationships, where is the good place to put code in?

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<table1>().HasMany(i => i.AllowedDestinations).WithMany(d => d.something)
      ......

Or other place other than OnModelCreating?

  • 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-15T07:49:01+00:00Added an answer on June 15, 2026 at 7:49 am

    You can create a class called “MYEntityConfiguration” (where MyEntity is the type of entity you are trying to configure) and inherit from the EntityTypeConfiguration<> type found in Entity Framework.

    For example….

    public class Position
        {
            public int Id { get; set; }
    
            public string CompanyName { get; set; }
    
            public bool IsCurrentRole { get; set; }
    
            public string Summary { get; set; }
    
            public string Title { get; set; }
    
            public int? StartMonth { get; set; }
    
            public int? StartYear { get; set; }
    
            public int? EndMonth { get; set; }
    
            public int? EndYear { get; set; }
    
            public virtual User User { get; set; }
            public int UserId { get; set; }
        }
    

    and the corresponding configuration file….

    public class PositionConfiguration : EntityTypeConfiguration<Position>
    {
        public PositionConfiguration()
        {
            this.ToTable("Positions");
            this.HasKey(p => p.Id);
            this.Property(p => p.Id).HasColumnName("PositionId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    
            this.Property(p => p.CompanyName).HasColumnName("CompanyName");
            this.Property(p => p.EndMonth).IsOptional().HasColumnName("EndMonth");
            this.Property(p => p.EndYear).IsOptional().HasColumnName("EndYear");
            this.Property(p => p.IsCurrentRole).IsOptional().HasColumnName("IsCurrentRole");
            this.Property(p => p.StartMonth).IsOptional().HasColumnName("StartMonth");
            this.Property(p => p.StartYear).IsOptional().HasColumnName("StartYear");
    
            this.Property(p => p.Summary).HasColumnName("Summary");
            this.Property(p => p.Title).HasColumnName("Title");
        }
    }
    

    Then in your DBContext… OnModelCreating…

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new PositionConfiguration());
    
                base.OnModelCreating(modelBuilder);
            }
    

    That will keep your configurations nice and contained… I found it to be the best way.

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

Sidebar

Related Questions

When using Entity Framework Code First 4.3.1 it is possible to create relationships with
I use Entity Framework code-first. DataBaseName - I want to create a database in
Entity Framework Code First will auto-create a table in the database base based on
I am using Entity Framework Code First to create a database table. My model
In Entity Framework code first, When I want to create a mapping for a
We are using Entity Framework Code First with Foreign Key relationships. We investigating on
I have an issue in entity framework code first, three table in database as
Hello everyone my problem is very simple. Using entity framework code first I want
Using Entity Framework Code First CTP5, how do I create a primary key column
I am using Entity Framework Code First. I want to query entites from database

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.