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

  • Home
  • SEARCH
  • 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 8220669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:36:41+00:00 2026-06-07T13:36:41+00:00

Very simply I am using Entity Framework 4.1 code first and I would like

  • 0

Very simply I am using Entity Framework 4.1 code first and I would like to replace my [ForeignKey(..)] attributes with fluent calls on modelBuilder instead. Something similar to WithRequired(..) and HasForeignKey(..) below which tie an explicit foreign key property (CreatedBySessionId) together with the associated navigation property (CreatedBySession). But I would like to do this for a one to one relationsip instead of a one to many:

modelBuilder.Entity<..>().HasMany(..).WithRequired(x => x.CreatedBySession).HasForeignKey(x => x.CreatedBySessionId)

A more concrete example is below. This works quite happily with the [ForeignKey(..)] attribute but I’d like to do away with it and configure it purely on modelbuilder.

public class VendorApplication
{
    public int VendorApplicationId { get; set; }

    public int CreatedBySessionId { get; set; }
    public virtual Session CreatedBySession { get; set; }
}

public class Session
{
    public int SessionId { get; set; }

    [ForeignKey("CurrentApplication")]
    public int? CurrentApplicationId { get; set; }
    public virtual VendorApplication CurrentApplication { get; set; }

    public virtual ICollection<VendorApplication> Applications { get; set; }
}

public class MyDataContext: DbContext
{
    public IDbSet<VendorApplication> Applications { get; set; }
    public IDbSet<Session> Sessions { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Session>().HasMany(x => x.Applications).WithRequired(x => x.CreatedBySession).HasForeignKey(x => x.CreatedBySessionId).WillCascadeOnDelete(false); 
        // Note: We have to  turn off Cascade delete on Session <-> VendorApplication relationship so that SQL doesn't complain about cyclic cascading deletes
    }
}

Here a Session can be responsible for creating many VendorApplications (Session.Applications), but a Session is working on at most one VendorApplication at a time (Session.CurrentApplication). I would like to tie the CurrentApplicationId property with the CurrentApplication navigation property in modelBuilder instead of via the [ForeignKey(..)] attribute.

Things I’ve Tried

When you remove the [ForeignKey(..)] attribute the CurrentApplication property generates a CurrentApplication_VendorApplicationId column in the database which is not tied to the CurrentApplicationId column.

I’ve tried explicitly mapping the relationship using the CurrentApplicationId column name as below, but obviously this generates an error because the database column name “CurrentApplicationId” is already being used by the property Session.CurrentApplicationId:

modelBuilder.Entity<Session>().HasOptional(x => x.CurrentApplication).WithOptionalDependent().Map(config => config.MapKey("CurrentApplicationId"));

It feels like I’m missing something very obvious here since all I want to do is perform the same operation that [ForeignKey(..)] does but within the model builder. Or is it a case that this is bad practise and was explicitly left out?

  • 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-07T13:36:42+00:00Added an answer on June 7, 2026 at 1:36 pm

    You need to map the relationship as one-to-many and omit the collection property in the relationship.

    modelBuilder.Entity<Session>()
       .HasOptional(x => x.CurrentApplication)
       .WithMany()
       .HasForeignKey(x => x.CurrentApplicationId)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone my problem is very simple. Using entity framework code first I want
When using the CTP 5 of Entity Framework code-first library (as announced here )
I am using Entity Framework 4.1 code first+MVC3 and the inheritence stratagy that I
I created very a simple database using Entity Framework 4. I'd like to be
I have created a very simple Entity Framework 4.0 model using VS2010 Professional. Ignore
I have a very simple repository I'm playing around with, using Entity Framework v4
I'm using a DB first approach, using Entity Framework and I've just read this
I am using Entity Frameworks Code First. I have one entity that I need
riting a very simple search using like and have an option to omit options,
I'm inserting a lot of data into SQL Server CE 4.0 using Entity Framework

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.