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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:33:39+00:00 2026-06-04T20:33:39+00:00

I have some models and I want to have a One to zero or

  • 0

I have some models and I want to have a “One to zero or one” association.
I searched and tried to make the association using Fluent API.
Here are my classes :

public partial class Bill
{

[Key]
public int Id { get; set; }
[Required]
public double Amount { get; set; }

public virtual DomainRegOrder DomainRegOrder { get; set; }

}

public partial class DomainRegOrder
{
[Key]
public int Id { get; set; }
[Required]
public string DomainName { get; set; }

public virtual Bill Bill { get; set; }
}

and in the my DataContext class :

public class DataContext : DbContext {

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

 modelBuilder.Entity<DomainRegOrder>()
 .HasOptional(x => x.Bill)
 .WithOptionalDependent();

 }

but when I Initialize the Database, in the Bills table, I will have two
columns named “DomainRegOrder_Id” and “DomainRegOrder_Id1“.

I tried to add the DomainRegOrderId in the Bill entity as bellow:

public int? DomainRegOrderId { get; set; }

But there will be a “DomainRegOrderId” and a “DomainRegOrder_Id” on the “Bills” table.

I also changed .WithOptionalDependent() to .WithOptionalPrincipal()
but no changes were made as far as I know!!

Would somebody help me to get rid of these duplicate foreign keys, please?

  • 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-04T20:33:40+00:00Added an answer on June 4, 2026 at 8:33 pm

    I believe you want something like this:

    modelBuilder.Entity<Bill>() 
      .HasOptional(x => x.DomainRegOrder) 
      .WithOptionalDependent(c => c.Bill).Map(p => p.MapKey("DomainRegOrderID"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some models witch are using Doctrine nestedset feature. I want to add
I have created a play application and have some models. One of them contains
I want get data from one of my models. I am using Entity datamodel
I have some code (that is working), but I just want to make sure
In my isometric flash game I have some models with long shadows. I want
I have some server startup code that is lying in the models.py of one
So say I have some classes X, Y and Z using SQLAlchemy declarative syntax
In my rails model I have some kind of template system. I want to
I have some models; Vocabularies (tags lists), Labels (tags) and different articles types. These
I have some models that are laid out like so (with a lot more

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.