Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > Designer> Database Schema name used within EntityName to differentiate naming conflicts
 

Pages: 1
Designer
Database Schema name used within EntityName to differentiate naming conflicts
Page:1/1 

  Print all messages in this thread  
Poster Message
raxuser100
User



Location:
Miami, USA
Joined on:
17-Dec-2019 12:43:51
Posted:
6 posts
# Posted on: 17-Dec-2019 12:52:46.  
How is it possible to use the Database schema name within a generated C# Entity Name to prevent duplicate database table name conflicts. Also to reference tables in different Db Schemas:

Example in MS SQL, we have two tables named tUser but they are in different schemas:
Also there is a [Customer].[tCustomerType] that is in the Schema [Customer]. Both [tUser] tables reference this as a FK

Code:
[Finance].[tUser]
[Marketing].[tUser]
[Customer].[tCustomerType]

-- With FK relations across Db Schemas
Code:
[Finance].[tUser] m-->1 [Customer].[tCustomerType]
[Marketing].[tUser] m-->1 [Customer].[tCustomerType]



In the generated C# code for Dtos I need the following output to add a Db Schema Name prefix to classes. Also the Dtos can reference tables in any schema:

Code:
public class Finance_User
{
int Id {get;set;}

// 1 to many - FK to [Customer].[tCustomerType] table
Customer_CustomerType CustomerTypePrimary {get;set;}

}

public class Marketing_User
{
int Id {get;set;}

// 1 to many - FK to [Customer].[tCustomerType] table
Customer_CustomerType CustomerTypePrimary {get;set;}

}


The suggested solution was for all tables in a Schema to be added into an EntityGroup, then to output these as separate projects. Unfortunately the Dtos in different projects cannot reference each other. This is stated in the documentation

https://www.llblgen.com/Documentation/5.6/Designer/Functionality%20Reference/ElementGrouping.htm
  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14637 posts
# Posted on: 18-Dec-2019 05:22:44.  
You could use Entity Model element grouping. You have to set the Group Usage project setting to AsSeparateProjects then add all the tables in schema A to model group Dev, and all tables in schema B to model group Prod. LLBLGen Pro will generate code for each group separately, instead of once for the whole project.

  Top
raxuser100
User



Location:
Miami, USA
Joined on:
17-Dec-2019 12:43:51
Posted:
6 posts
# Posted on: 10-Jan-2020 14:58:08.  
Thanks for the suggestion. Unfortunately for this case, it does not work fully.

In some FK's the tables span across multiple DB Schemas.

Separate Projects per Entity Group does not allow FK's to span across multiple Projects.
  Top
daelmo
Support Team



Location:
Guatemala City
Joined on:
28-Nov-2005 23:35:24
Posted:
8148 posts
# Posted on: 13-Jan-2020 06:43:38.  
My recomendation would be: When you add those duplicate names on the Reverse Engineer wizard, assign different names to those entities, like: FinanceUser, MarketingUser. That way your names are clean from model to code.

If there are a lot of those duplicates, you could write a plugin for that.


David Elizondo
LLBLGen'ing (articles and code snippets) | linkedin | twitter
 
Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
38081 posts
# Posted on: 13-Jan-2020 09:22:06.  
For v5.7 we have planned to solve this using a pattern for reverse engineering (e.g. {schemaname}_{tablename}, but that doesn't help you at the moment, so for now you have to fall back to e.g. manually rename them once (the name isn't reset the next time)
Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
raxuser100
User



Location:
Miami, USA
Joined on:
17-Dec-2019 12:43:51
Posted:
6 posts
# Posted on: 13-Jan-2020 11:21:27.  
Thanks David and Frans.

I will most likely just rename the tables manually which should be retained after future syncs with the Db.

Would you happen to have a sample C# plugin that I could use to automate and add some custom logic please?

Look forward to v5.7!


  Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
38081 posts
# Posted on: 13-Jan-2020 11:54:26.  
raxuser100 wrote:
Thanks David and Frans.

I will most likely just rename the tables manually which should be retained after future syncs with the Db.

Would you happen to have a sample C# plugin that I could use to automate and add some custom logic please?

Look forward to v5.7!

If it's a one-time thing, you can use the element search in the designer. It allows you to run any C# against the entity model.

Open element search, select 'Entity' as element type and paste the following (I use SQL Server here, the driver id of your db is in the first code pane, so you have to adjust it for that)

Code:

var cmd = new UndoablePeriodCommand("Renaming elements");
CommandQueueManagerSingleton.GetInstance().BeginUndoablePeriod(cmd);
foreach(EntityDefinition e in p.EntityModel.Vertices)
{
    var mapping = p.GetGroupableModelElementMapping(e, SqlServerSqlClientDriverID, false);
    if(mapping==null || mapping.MappedTarget==null)
    {
        continue;
    }
    e.Name = string.Format("{0}{1}", mapping.MappedTarget.ContainingSchema.SchemaOwner, e.Name);
}
CommandQueueManagerSingleton.GetInstance().EndUndoablePeriod(cmd);
return p.EntityModel.Vertices;

This will prefix all names in the entity model with the schema name.
You can limit the elements you want with filtering p.EntityModel.Vertices (which are all entities in the model) based on a subset, e.g. a list of names you hardcode, e.g. to avoid having all names prefixed and only the ones you need which are duplicates (like your User example).
It also has undo / redo awareness, so you can undo the action in one go. It might appear a bit slow, that's caused by the fact that you're renaming elements one by one and the UI will get notified by that and will re-render the element in the project explorer...

Renaming the tables is best done in the database itself, as some databases require to drop/recreate renamed tables so the provided tooling for the database can help there. Then you can simply sync your project in the designer with the database and the designer should pick up renamed elements (it will do heuristics for renamed elements).

If you want to write a plugin, you can look at the sourcecode archive on the website which comes with the sourcecode of a couple of plugins, and the SDK:
https://www.llblgen.com/Documentation/5.6/SDK/gui_implementingplugin.htm

Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
raxuser100
User



Location:
Miami, USA
Joined on:
17-Dec-2019 12:43:51
Posted:
6 posts
# Posted on: 13-Jan-2020 13:55:48.  
Thanks so much for the wonderful support, I will create the Plugin and automate this out!

  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.