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

Enum from tables and replace props referencing it #777

Closed McDoit closed 1 year ago

McDoit commented 1 year ago

I'm trying to achieve a workflow where i generate Enums from tables, find props in entities having a FK there and repalce the prop with the enum and then remove the navigation property from that entity

So a bit like this:

Table api.Types into a enum type like this (with AddEnum)

public enum ApiTypesEnum
{
        Facebook = 1,
        Instagram = 2,
}

and turn entity for table api.Users

public class ApiUserEntity
{
        public int Id { get; set; } // ID (Primary key)
        public int ApiTypeId { get; set; } // FKApiTypeID

        public virtual ApiTypeEntity ApiUserType { get; set; } // FK__Users__FKApiTyp__72D05FB5
}

into this

public class ApiUserEntity
{
        public int Id { get; set; } // ID (Primary key)
        public ApiTypesEnum ApiTypeId { get; set; } // FKApiTypeID
}

As the enum tables are just there for db constraints and dont want them as a navigation property

My problem is that AddEnum is called after UpdateColumnand similar calls, so I cant use the enums i generate in AddEnum And a problem I have is that the FkEntity property on column is empty, dunno if it is related to some other changes I made

Is there any better or other way to achieve this that I have missed?

sjh37 commented 1 year ago

Hi Erik,

The following callback should help you:

// Use the following list to replace column byte types with Enums.
// As long as the type can be mapped to your new type, all is well.
Settings.AddEnumDefinitions = delegate(List<EnumDefinition> enumDefinitions)
{
    enumDefinitions.Add(new EnumDefinition { Schema = "dbo", Table = "ApiTypes", Column = "Id", EnumType = "ApiTypesEnum" });
};
McDoit commented 1 year ago

But then I can't use automatic filtering/adding of Enums as in AddEnum from a Table

Best would be to be able to add EnumDefinition from the tables targeting a Table i want defined as an Enum

But i don't know if its possible in how the script currently work, and the order parts are executed

McDoit commented 1 year ago

@sjh37 I can kinda work around it, but then I need to have some extra Lists to temp store data in between the calls What wold be the best way to do that? As it is now, if I define var enumTables = new List<string>(); it doesn't seem to be accessible from other delegates?

sjh37 commented 1 year ago

You could add an enumTables list to the Settings class located at the top of the EF.Reverse.POCO.v3.ttinclude file. I'll have a play and report back.

sjh37 commented 1 year ago

Add EnumTables to the top of the settings class like so:

public static class Settings
{
    public static List<string> EnumTables = new();
    ...
}

And you can use that anywhere in your .tt file like so:

Settings.EnumTables.Add("todo");
sjh37 commented 1 year ago

I will close this issue. Any problems, please let me know here.