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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:53:40+00:00 2026-06-17T15:53:40+00:00

Some foreign key’s are created double in my contract table. (Article and Client). And

  • 0

Some foreign key’s are created double in my contract table. (Article and Client). And Company is ok!

My models:

public class Contract {
    [Key]
    public int ContractID { get; set; }
    public double PricePerUnit { get; set; }
    public int Unit { get; set; }
    public int Currency { get; set; }

    [Required]
    public int ClientID { get; set; }
    public virtual Client Client { get; set; }

    [Required]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }

    [Required]
    public int ArticleID { get; set; }
    public virtual Article Article { get; set; }

}

public class Client {
    [Key]
    public int ClientID { get; set; }
    public string Number { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ZipCode { get; set; }
    public string City { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string Memo { get; set; }
    public bool isMerchant { get; set; }

    public string Name
    {
        get
        {
            return string.Format("{0} {1}", FirstName, LastName);
        }
    }

    //[Required]
    public int? MerchantReferenceID { get; set; }
    public virtual Client MerchantReference { get; set; }
    [Required]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

public class Company
{
    [Key]
    public int CompanyID { get; set; }
    public string Name { get; set; }
    public int DeviceIncomingWeight { get; set; }
    public string ZipCode { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public bool Admin { get; set; }
    public int UnitForMeasurements { get; set; }
    public int UnitForDisplayOnDocuments { get; set; }

    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
    public virtual ICollection<Article> Articles { get; set; }
    public virtual ICollection<Client> Clients { get; set; }
    public virtual ICollection<Location> Locations { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual ICollection<IncomingMeasurement> IncomingMeasurements { get; set; }
    public virtual ICollection<Measurement> Measurements { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

public class Article {
    [Key]
    public int ArticleID { get; set; }
    [Required]
    public string Code { get; set; }
    public string Name { get; set; }
    public bool TrackStock { get; set; }
    public int CurrentStock { get; set; }
    public double? Price { get; set; }

    [Required]
    public int CompanyID { get; set; }
    public virtual Company Company { get; set; }
    [Required]
    public int CategoryID { get; set; }
    public virtual Category Category { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

This is my OnModelCreating, where probably the fault lies:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       // modelBuilder.Entity<Contract>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Contract>().HasRequired(bm => bm.Article).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Contract>().HasRequired(bm => bm.Client ).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Article>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Measurement>().HasRequired(bm => bm.Company).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Order>().HasRequired(bm => bm.Client).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Order>().HasRequired(bm => bm.Article).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<IncomingMeasurement>().HasRequired(bm => bm.client).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<Client>().HasOptional(c => c.MerchantReference).WithMany().HasForeignKey(c => c.MerchantReferenceID);

        //Required fields


        base.OnModelCreating(modelBuilder);
    }

And there is something weird happening in my db (my sql server), namely this is my create table schema.

These are my fields:

CREATE TABLE [dbo].[Contracts](
[ContractID] [int] IDENTITY(1,1) NOT NULL,
[PricePerUnit] [float] NOT NULL,
[Unit] [int] NOT NULL,
[Currency] [int] NOT NULL,
[ClientID] [int] NOT NULL,
[CompanyID] [int] NOT NULL,
[ArticleID] [int] NOT NULL,
[Client_ClientID] [int] NOT NULL,
[Article_ArticleID] [int] NOT NULL,
[Client_ClientID1] [int] NULL,
[Article_ArticleID1] [int] NULL,

If you notice it, [Client_ClientID] has a duplicate: [Client_ClientID1] and also [Article_ArticleID] in [Article_ArticleID1].
But company doesn’t.

Any thoughts on how to fix this?

  • 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-17T15:53:41+00:00Added an answer on June 17, 2026 at 3:53 pm

    This happens because you include a redundant (foreign key) column in your entity classes. For example look at the category in your Contract class.

    public class Contract
    {
        public Int32 CategoryID { get; set; }
    
        public virtual Category Category { get; set; }
    }
    

    You manually specify a property and therefore column CategoryID and then the Entity Framework generates another column to hold the foreign key for the Category referenced by the property Category.

    So just remove the property CategoryID and use contract.Category.CategoryID instead if you need the ID of the referenced category.

    UPDATE

    I was not aware of the suggestion to include a foreign key property but looking at the article linked in the comment to Jeff Siever’s answer I probably spotted the answer in the section Configuring Unconventional Foreign Key Names.

    The Entity Framework uses a convention to match the name of the navigation property and the foreign key property and the default convention is either NavigationPropertyNameId or NavigationPropertyName_Id while you use NavigationPropertyNameID with uppercase D.

    So you have several options – change your naming to use Id, replace the convention or override the convention.

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

Sidebar

Related Questions

We have some models that are have a user as a foreign key. But
trying to load some data into a table that has a foreign key constraint.
Suppose in a table 'Foo' I have a foreign key 'barId' to some table
We're designing a database in which I need to consider some FK(foreign key) constraints.
I have some innoDbs with only 2 int columns which are foreign keys to
When I delete an item which triggers some foreign key cascade-deletes, is there a
added a foreign key relationship to a table in order to do so I
Is it not possible that foreign key(single column) in a child table references to
I have a new table that has foreign key references to a number of
I'm trying to pre-fetch some foreign key data using a linq query. A quick

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.