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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:19:40+00:00 2026-05-22T01:19:40+00:00

class First { [Key] public int Id { get; set; } } class Second

  • 0
class First
{
    [Key]
    public int Id { get; set; }
}

class Second
{
    [Key]
    public int Id { get; set; }

    public int? First_Id { get; set; }

    [ForeignKey("First_Id")]
    public First First { get; set; }
}

public class SecondMapping : EntityTypeConfiguration<Second>
{
    public SecondMapping () 
        : base()
    {
        this.HasOptional(s => s.First)
            .With ... ???
    }
}

Second may have a reference to First. But First never has a reference to Second. Is it possible to apply this mapping with Entity Framework 4.1?

EDIT:
Previously, that was my solution:

this.HasOptional(s => s.First)
    .WithOptionalDependent()
    .WillCascadeOnDelete(false);

Second could contain one instance of First (dependent on some kind of Usage-Attribute). First doesn’t contain any instance of Second.

  • 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-22T01:19:40+00:00Added an answer on May 22, 2026 at 1:19 am

    One-to-one relation is possible only if foreign key is also primary key of dependent entity. So the correct mapping is:

    class First
    {
        [Key]
        public int Id { get; set; }
    }
    
    class Second
    {
        [Key, ForeignKey("First")]
        public int Id { get; set; }
        public First First { get; set; }
    }
    

    The reason is that to enforce one-to-one relation in the database foreign key must be unique in the Second entity. But entity framework doesn’t support unique keys – the only unique value for EF is primary key. This is limitation of EF.

    There is workaround described on Morteza Manavi’s blog. Workaround is based on mapping association as one-to-many and enforcing uniqueness in database by introducing unique constraints in custom database initializer.

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

Sidebar

Related Questions

I have this entity: public class MyEntity { [Key] public int Id { get;
Take this class for example: public class Applicant : UniClass<Applicant> { [Key] public int
I defined a model like this public class Planilla { [Key] public int IDPlanilla
Lets say I have this amputated Person class: class Person { public int Age
for example i've got a TestItem entity: public class TestItem { [Key] public int
I have a code first model: namespace InternetComicsDatabase.Models { public class Issue { [Key]
I have an example class book : public class Book { [Key] public int
I have the following models: public class Device { [Key] public int ID {
I have a .py file with lots of classes: class First(Second): #code class Third(Fourth):
Using Dozer to map two objects, I have: /** /* This first class uses

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.