EntityFramework-Reverse-POCO-Code-First-Generator icon indicating copy to clipboard operation
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard

Two tables having one property with the same name and their relationship is related to the same table. Model generation issue

Open fxvits opened this issue 9 years ago • 10 comments

Hi guys,

I have two tables User and Attendee both having PhoneCountryId property linked to a Country Table. So in SQL Schema, I get 2 relationships : FK_User_Country and FK_Attendee_Country. Once I generate the model I get a strange behavior. The reverse navigation properties in Country Table seem to be well defined as PhoneCountry but badly defined as Users and the same for the foreignKeys, in Attendee Table this is well defined as PhoneCountry but badly defined in User Table as Country. Did I miss something ?

Help will be much appreciated.

public class Country
    {
        public int CountryId { get; set; } // CountryID (Primary key)
        public string Code { get; set; } // Code (length: 12)
    
      // Reverse navigation
      /// <summary>
      /// Child Attendees where [Attendee].[PhoneCountryID] point to this entity (FK_Attendee_Country)
        /// </summary>
        public virtual System.Collections.Generic.ICollection<Attendee> PhoneCountry { get; set; } // Attendee.FK_Attendee_Country

        /// <summary>
        /// Child Users where [User].[PhoneCountryID] point to this entity (FK_User_Country)
        /// </summary>
        public virtual System.Collections.Generic.ICollection<User> Users { get; set; } // User.FK_User_Country

public Country()
        {
            PhoneCountry = new System.Collections.Generic.List<Attendee>();
            Users = new System.Collections.Generic.List<User>();
         }
}

// Attendee   
 [System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.32.0.0")]
    public class Attendee   
    {
        public long AttendeeId { get; set; } // AttendeeID (Primary key)
        public int? PhoneCountryId { get; set; } // PhoneCountryID
 
       // Foreign keys

         /// <summary>
        /// Parent Country pointed by [Attendee].([PhoneCountryId]) (FK_Attendee_Country)
        /// </summary>
        public virtual Country PhoneCountry { get; set; } // FK_Attendee_Country
}

// User
    [System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.32.0.0")]
    public  class User
    {
        public long UserId { get; set; } // UserID (Primary key)
        public int? PhoneCountryId { get; set; } // PhoneCountryID

      // Foreign keys

       /// <summary>
        /// Parent Country pointed by [User].([PhoneCountryId]) (FK_User_Country )
        /// </summary>
        public virtual Country Country { get; set; } // FK_User_Country 
}

fxvits avatar Jul 12 '17 09:07 fxvits

Can you supply me with the SQL for these tables and I will test this myself.

sjh37 avatar Jul 12 '17 17:07 sjh37

Thank you for your fast answer.

Find in attachment the requested SQL Script. Hope It will help.

I have also another question. The Poco Generator is converting sql_variant fields to string properties. Is it possible to get (transform) these properties to return a type "object" instead of string ? SqlScriptReversePoco.zip

fxvits avatar Jul 13 '17 09:07 fxvits

This is what I get generated for me using the SQL you gave me:

// Attendee
[System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.32.0.0")]
public class Attendee
{
    public long AttendeeId { get; set; } // AttendeeID (Primary key)
    public string Lastname { get; set; } // Lastname (length: 50)
    public string Firstname { get; set; } // Firstname (length: 50)
    public int? PhoneCountryId { get; set; } // PhoneCountryID

    // Foreign keys

    /// <summary>
    /// Parent Country pointed by [Attendee].([PhoneCountryId]) (FK_Attendee_PhoneCountry)
    /// </summary>
    public virtual Country Country { get; set; } // FK_Attendee_PhoneCountry
}

// Country
[System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.32.0.0")]
public class Country
{
    public int CountryId { get; set; } // CountryID (Primary key)
    public string Code { get; set; } // Code (length: 12)

    // Reverse navigation

    /// <summary>
    /// Child Attendees where [Attendee].[PhoneCountryID] point to this entity (FK_Attendee_PhoneCountry)
    /// </summary>
    public virtual System.Collections.Generic.ICollection<Attendee> Attendees { get; set; } // Attendee.FK_Attendee_PhoneCountry
    /// <summary>
    /// Child Users where [User].[PhoneCountryID] point to this entity (FK_User_PhoneCountry)
    /// </summary>
    public virtual System.Collections.Generic.ICollection<User> Users { get; set; } // User.FK_User_PhoneCountry

    public Country()
    {
        Attendees = new System.Collections.Generic.List<Attendee>();
        Users = new System.Collections.Generic.List<User>();
    }
}

// User
[System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.32.0.0")]
public class User
{
    public long UserId { get; set; } // UserID (Primary key)
    public string Lastname { get; set; } // Lastname (length: 100)
    public string Firstname { get; set; } // Firstname (length: 100)
    public int? PhoneCountryId { get; set; } // PhoneCountryID

    // Foreign keys

    /// <summary>
    /// Parent Country pointed by [User].([PhoneCountryId]) (FK_User_PhoneCountry)
    /// </summary>
    public virtual Country Country { get; set; } // FK_User_PhoneCountry
}

sjh37 avatar Jul 13 '17 09:07 sjh37

The generated C# code looks ok given the SQL you gave me. Any ideas why your's is different?

sjh37 avatar Jul 17 '17 08:07 sjh37

Hi Simon, Sorry but I didn't have access to a computer for the moment that's why I didn't give you my feedback. Clearly, I'm curious to check why we didn't get the same generated code. I promise to come back you once I will be back at work.

fxvits avatar Jul 18 '17 06:07 fxvits

I'm experiencing the same behaviour - two tables where one table has two relationships to the other. I created a sample database and reproduced the issue - see attached files.

I think the problem is to do with the ForeignKeyName method; The second attempt at naming the property at the end of a foreign key uses the foreignKeyName value but this value is always the same, whether the current table is the primary or secondary. This makes sense for the foreignKey table, but not for the primary key table (which ends up with a property named after itself).

This is present in v2.33.

database.sql.txt

database.tt.txt

database.cs.txt

davidsk avatar Oct 10 '17 14:10 davidsk

@davidsk Can you add a primary key to t_Object_Data and let me know if that helps. try

ALTER TABLE t_Object_Data
ADD CONSTRAINT [PK_Object_Data] PRIMARY KEY CLUSTERED (Object_Data_Id)

sjh37 avatar Oct 10 '17 15:10 sjh37

No problem, first thing tomorrow. The actual table does have a composite primary on columns that I deleted for the sample.

Can you explain why that would affect the naming of a foreignKey / reverse navigation property? Object_Data_Id doesn't participate in the relationships at all.

davidsk avatar Oct 10 '17 19:10 davidsk

Adding a primary key doesn't have any effect. I think I understand the issue though - GetUniqueColumnName is used to generate unqiue property names for both sides of a relationship, the Pk side and the Fk side. However, it only ever generates a value (fkName) based on the FkColumnName (which generally refers to the PkTable) and passes this to the ForeignKeyName function.

When there are two relationships, the ForeignKeyName function will necessarily fall through to the second case statement which uses the provided value (foreignKeyName) as an input which returns the same value to both the Pk object and Fk object.

The most immediate way around this is to only call ForeignKeyName with attempt 2 or 3 when the current table Name equals the foreignKey.FkTableName.

// Attempt 1
// ...

if(Name == foreignKey.FkTableName)
{
    // Attempt 2
    // ...
    // Attempt 3
    // ...
}			

// Attempt 4

I hope that makes sense. Let me know if not.

davidsk avatar Oct 11 '17 07:10 davidsk

The naming fault identified above (attempts 2 and 3 being applied on the principal side) was fixed in October 2017; those attempts now only run for the table that owns the foreign key. Settings.ForeignKeyName gives full control if the defaults still do not suit. Closing.

sjh37 avatar Sep 24 '26 10:09 sjh37