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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:20:54+00:00 2026-06-15T04:20:54+00:00

In Entity Framework code first, When I want to create a mapping for a

  • 0

In Entity Framework code first, When I want to create a mapping for a normal one-to-many foreign key, I use

HasRequired(a=>a.County)
  .WithMay(b=>b.Cities)
  .HasForeignKey(a=>a.CountCode);

Now when I instead try to create a one-to-one-mapping, it will become something like this:

HasRequired(a=>a.County)
  .WithOptional(b=>b.ConnectedCounty)
  .Map(a=>a.MapKey("CountyCode"));

Why do I have to use a string like CountyCode in this expression? Is there a way to create the mapping not having strings, just linq expressions?

  • 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-15T04:20:55+00:00Added an answer on June 15, 2026 at 4:20 am

    The reason is that you are not supposed to use this when you define one-to-one relationship because by using this you will get back to defining one-to-many relationship in the database. EF uses real one-to-one relationship only when FK in dependent entity is also its PK. That is the only situation when you can be sure that principal entity will never be referenced by more than one dependent entity (because EF doesn’t support database unique keys).

    If you use your code you will define separate FK and it will “work” but there will be no automatically created unique key on the column in the database.

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

Sidebar

Related Questions

I use Entity Framework code-first. DataBaseName - I want to create a database in
We are using Entity Framework Code First with Foreign Key relationships. We investigating on
A one-many or many-many relationship in Entity Framework Code First looks like this:- public
Entity Framework Code First: Suppose I want to create table relationships, where is the
Hello everyone my problem is very simple. Using entity framework code first I want
(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually
When using Entity Framework Code First 4.3.1 it is possible to create relationships with
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I am using the latest Entity Framework with a code-first approach to create a
I am trying to use MVC3 with the Entity framework with Code First, but

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.