C# EntityCode First : Unwanted foreign key created

Viewed 93

I have a table of "Statements" that is related to both :

  • a table of "Transactions" (with a foreign key on IdStatement)
  • a table of "SpecificTransactions". The class "SpecificTransaction" inherits from the table "Transaction"

This is the existing code First Model :

  1. Table Statement

    [Serializable]
    [Table("Statement", Schema = "dbo")]
    public class Statement
    {
        public Statement()
        {
            this.Transactions = new List<Transaction>();
            this.SpecificTransactions = new List<SpecificTransaction>();
        }
    
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int IdStatement { get; set; }
    
        public List<Transaction> Transactions { get; set; }
        public List<SpecificTransaction> SpecificTransactions { get; set; }
    
        [... Other properties ...]
    }
    
  2. Table Transaction

    [Serializable]
    [Table("Transaction", Schema = "dbo")]
    public class Transaction
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int IdTransaction { get; set; }
    
        [ForeignKey("Statement")]
        public Nullable<int> IdStatement { get; set; }
    
        [... Other properties ...]
    
    }
    
  3. Table SpecificTransaction (inherits from Transaction)

    [Serializable]
    [Table("SpecificTransaction", Schema = "dbo")]
    public class SpecificTransaction : Transaction
    {
    
        [StringLength(255)]
        public string UniqueValue { get; set; }
    
        [StringLength(50)]
        public string Status { get; set; }
    
        [StringLength(50)]
        public string Hash { get; set; }
    
        [... Other properties ...]
    
    }
    

When the database is being created, an unwanted field (i.e column) is added on my table SpecificTransaction, called "Statement_IdStatement".

When I try to add my Statement containing SpecificTransactions, none of the following columns :

  • Table Transaction > Column IdStatement
  • Table SpecificTransaction > Column Statement_IdStatement

Is being populated by the IdStatement, I have to add the Id afterwards.

Furthermore, when I assign the "IdStatement" on my table "SpecificTransaction", only the "Statement_IdStatement" column is filled, and not the "IdStatement" of my table Statement.

Is there a way to :

  • avoid the creation of the column "Statement_IdStatement" and keeping my EF Code First class that way ? (or at least, have no change on the tables Statement and Transaction)

  • When I add my Statements containing SpecificTransactions and I saveChanges, the column "Transaction" > "IdStatement" is automatically populated ?

Thanks in advance.

1 Answers

You are getting the extra column Statement_IdStatement because you have declared a second relationship, directly between the Statement and SpecificTransaction model types, namely, by the property public List<SpecificTransaction> SpecificTransactions on your Statement class. This is enough to cause Entity Framework to create an extra table column to map that one-to-many relationship to, and that's why it gets filled. It doesn't need to be mapped to an extra foreign-key model property for this to occur.

You probably don't actually need that extra property on Statement. You can actually insert SpecificTransaction objects into the Transactions list property, and the mapping will be taken care of for you.

You may have declared this property in order to be able to access only the SpecificTransaction objects for your Statement. If this was your intent, you can alternatively access them using myStatement.Transactions.OfType<SpecificTransaction>().

Related