sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
700 stars 230 forks source link

Generating a property type as a Enumeration from a foreign key #748

Closed James00-Fast closed 2 years ago

James00-Fast commented 2 years ago

Has anyone experienced this issue ?

The relationship from 'Common_TestABC.Common_Status' to 'Common_Status.Common_TestAbcs' with foreign key properties {'StatusId' : StatusTest} cannot target the primary key {'StatusId' : int} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship.

We are trying to generate a class from SQL. The table TestABC has a foreign key reference called StatusId to the Status table. Every time we startup the solution we receive the above error. This happens for any table we attempt. The idea is to be able to use the generation in a way TestABC.StatusId = Status (Enum). Rather than creating our own Enums by hand.

I have, among others, tried EnumerationSettings & enumDefinitions. Running out of options. The .tt looks fine. Is this a known issue ?

new EnumerationSettings
{
    Name       = "StatusTest",
    Table      = "Common.Status",
    NameField  = "Description",
    ValueField = "StatusId"
}

enumDefinitions.Add(new EnumDefinition { Schema = "Common", Table = "TestABC", Column = "StatusId", EnumType = "StatusTest" });
James00-Fast commented 2 years ago

Simple table layout example.

image

image

sjh37 commented 2 years ago

Sorry for the error! In class Common_Status, change the PK to also be the enum as below:

// Status
public class Common_Status
{
    public StatusTest StatusId { get; set; } // StatusId (Primary key)   <<<========== now an enum
    public string Description { get; set; } // Description (length: 50)

    // Reverse navigation

    /// <summary>
    /// Child Common_TestAbcs where [TestABC].[StatusId] point to this entity (FkNameAbc)
    /// </summary>
    public virtual ICollection<Common_TestAbc> Common_TestAbcs { get; set; } // TestABC.FkNameAbc

    public Common_Status()
    {
        Common_TestAbcs = new List<Common_TestAbc>();
    }
}

I will mention this case when the fix is in.

sjh37 commented 2 years ago

SQL to generate tables above:

CREATE TABLE Common.[Status]
(
    StatusId INT IDENTITY(1, 1) NOT NULL,
    [Description] NVARCHAR(50) NOT NULL,
    CONSTRAINT PK_Status
        PRIMARY KEY CLUSTERED (StatusId ASC)
);
GO
CREATE TABLE Common.TestABC
(
    TestABCId INT IDENTITY(1, 1) NOT NULL,
    StatusId INT NOT NULL,
    FieldA VARCHAR(1) NULL,
    FieldB BIT NULL,
    CONSTRAINT PK_TestABC
        PRIMARY KEY CLUSTERED (TestABCId ASC),
    CONSTRAINT FkNameAbc
        FOREIGN KEY (StatusId)
        REFERENCES common.[Status] (StatusId)
);
GO
INSERT INTO Common.[Status] ([Description])
VALUES ('Ok'),('Info'),('Warning'),('Error'),('Critical');
GO
James00-Fast commented 2 years ago

@sjh37 Thank you very much ! Your fast response and effort is appreciated. This worked for us. Added an extra enumDefinitions liked you mentioned 🙌.

sjh37 commented 2 years ago

Now released in 3.6.1