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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:37:38+00:00 2026-06-08T02:37:38+00:00

I’m using the following classes for a one-to-many relationship: public class Supplier { public

  • 0

I’m using the following classes for a one-to-many relationship:

public class Supplier
{
    public Supplier()
    {
        SupplierId = Guid.NewGuid();
    }

    public Guid SupplierId { get; set; }
    [Required]
    public string Name { get; set; }

    [Required]
    public virtual Address Address { get; set; }

    public virtual ICollection<Contact> Contacts { get; set; }
}

public class Contact
{
    public Contact()
    {
        ContactId = Guid.NewGuid();
    }

    public Guid ContactId { get; set; }

    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }

    [Required]
    public Guid SupplierId { get; set; }
    [ForeignKey("SupplierId")]
    public virtual Supplier Supplier { get; set; }
}

I’m using ASP.NET MVC 4 and upon Create of a new Contact, the SupplierId is passed in the querystring and added to the Contact create form as a hidden field:

@Html.HiddenFor(model => model.SupplierId)

This creates the Contact successfully and populate’s the Contact’s SupplierId field with the Guid from the query string. However, the problem is that the Contacts table has another field for the virtual Supplier relationship called Supplier_SupplierId and this field is NULL. I’m not sure I understand why Supplier_SupplierId is even being created on the Contacts table, as SupplierId should be being used as the foreign key and this would just be redundant. If I try to access the Contacts collection of a Supplier I successfully added a Contact to, the collection comes back empty because Contact.Supplier_SupplierId is NULL. However, if I copy the value from the SupplierId column into the Supplier_SupplierId column of the Contact record, Supplier.Contacts will bring back the Contact.

This is what my Contacts table ends up looking like:

CREATE TABLE [dbo].[Contacts] (
    [ContactId]           UNIQUEIDENTIFIER NOT NULL,
    [FirstName]           NVARCHAR (MAX)   NOT NULL,
    [LastName]            NVARCHAR (MAX)   NOT NULL,
    [Supplier_SupplierId] UNIQUEIDENTIFIER NULL,
    CONSTRAINT [PK_dbo.Contacts] PRIMARY KEY CLUSTERED ([ContactId] ASC),
    CONSTRAINT [FK_dbo.Contacts_dbo.Suppliers_SupplierId] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Suppliers] ([SupplierId]),
    CONSTRAINT [FK_dbo.Contacts_dbo.Suppliers_Supplier_SupplierId] FOREIGN KEY ([Supplier_SupplierId]) REFERENCES [dbo].[Suppliers] ([SupplierId])
);

Does anyone know why 2 foreign keys to Suppliers are being created in Contacts? How can I change it so that the only foreign key to Suppliers is SupplierId?

  • 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-08T02:37:40+00:00Added an answer on June 8, 2026 at 2:37 am

    This mapping is wrong (I’m refering to your comment below your question):

    modelBuilder.Entity<Contact>()
        .HasRequired(r => r.Supplier)
        .WithMany()
        .HasForeignKey(f => f.SupplierId)
        .WillCascadeOnDelete(false);
    

    You need:

    modelBuilder.Entity<Contact>()
        .HasRequired(r => r.Supplier)
        .WithMany(s => s.Contacts)
        .HasForeignKey(f => f.SupplierId)
        .WillCascadeOnDelete(false);
    

    Otherwise EF will consider Supplier.Contacts as a navigation property that belongs to another and second one-to-many relationship between Supplier and Contact – and that introduces the second foreign key.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.