EntityFramework-Reverse-POCO-Code-First-Generator
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard
How can I generate fully qualified types?
I have a database with a table named TimeZone. I have disabled pluralization as it is undesired in my case. When I generate the DbContext, there is an ambiguous reference between MyCustomNamespace.TimeZone and System.TimeZone. I noticed that none of the class references are fully qualified (include namespaces). How can I have the generated classes use fully qualified namespaces to avoid the ambiguous reference?
Great question.
First off, I would simply try editing EF.Reverse.POCO.v3.ttinclude and search for:
public static readonly List<string> ReservedKeywords = new List<string>
And add in "TimeZone".
The table would end up being called TimeZone1, not ideal, but quick and simple.
You can see that happen in the code by searching for // Check for table or C# name clashes
Another solution is to prefix the table names with the schema, you can force "dbo" by changing:
Settings.DefaultSchema = DefaultSchema(conn);
for
Settings.DefaultSchema = "";
I don't like that as a) it would change the name of all the tables, and b) looks nasty.
To prefix the tables with a namespace:
Step 1: Change the DbSet<tablename>. In the TableTemplateData ctor, change DbSetName to be:
DbSetName = Settings.Namespace + "." + table.NameHumanCaseWithSuffix();
Step 2: Search for public CodeOutput GeneratePocoConfiguration(Table table)
And change
NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(),
to be
NameHumanCaseWithSuffix = Settings.Namespace + "." + table.NameHumanCaseWithSuffix(),
That should hopefully be it. Please let me know if it worked or not :-)
Thank you for your prompt reply!
So there were 5 lines I had to change in the v3.ttinclude file. I was only able to use Settings.Namespace in 1 of the 5 changes since my POCOs are in a different assembly/namespace:
Step 1: In the GeneratePoco method I had to change:
NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(),
to
NameHumanCaseWithSuffix = "MyCustomNamespace." + table.NameHumanCaseWithSuffix(),
Step 2: In the GeneratePocoConfiguration method I had to change:
NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(),
to
NameHumanCaseWithSuffix = "MyCustomNamespace." + table.NameHumanCaseWithSuffix(),
Step 3: In the IndexModelBuilder method I had to change:
sb.AppendLine($" modelBuilder.Entity<{c.ParentTable.NameHumanCaseWithSuffix()}>()");
to
sb.AppendLine($" modelBuilder.Entity<{"MyCustomNamespace." + c.ParentTable.NameHumanCaseWithSuffix()}>()");
Step 4: In the TableTemplateData ctor I had to change:
DbSetName = table.NameHumanCaseWithSuffix();
DbSetConfigName = table.NameHumanCaseWithSuffix() + Settings.ConfigurationClassName;
to
DbSetName = "MyCustomNamespace." + table.NameHumanCaseWithSuffix();
DbSetConfigName = Settings.Namespace + "." + table.NameHumanCaseWithSuffix() + Settings.ConfigurationClassName;
It would be great to have this feature built-in! It doesn't even need to be configurable since there isn't really a reason (I can think of) why you wouldn't want the generated types to always be fully qualified. Thanks for your help and consideration for adding this.
Thanks so much @udlose . I will implement this for the next release.