changing tables once on the DatabaseSchema

May 11, 2013 at 1:57 AM
sometimes the pluralization does not fir the one for Entity Framework.

is there a way to alter the name of the database

I have tried this
1-http://grab.by/mrle
2-also crazy things like this http://grab.by/mrlk

but no luck.

also we can use this to (for example) always remove the SQL Server sys-diagram table
Coordinator
May 11, 2013 at 12:12 PM
It is possible to just set the databasetable.NetName property. This is sufficient for simple changes before code generation.
var docStatusTable = schema.FindTableByName("DocumentStatus");
docStatusTable.NetName = "DocState";
var productsTableAgain = schema.FindTableByName("DocumentStatus"); //find it again (second reference)
Console.WriteLine(docStatusTableAgain.NetName); //shows "DocState"
Changing the actual name (databasetable.Name) is more problematic, although this is only a problem for DDL generation, not code generation. The foreign key constraints may keep the old name (in the string properties- TableName) although the reference properties will be correct. There are also references in views, stored procedures, triggers and the like- and those are way beyond the scope of this little project.

For more extensive changes, there is a mechanism via CodeWriterSettings - it has a Namer property, and you can either implement the INamer interface, or more practically override the base Namer class with customizations. The simplest is to override the Namer.Name(string) signature. In the library, the PluralizingNamer is in fact a class that inherits from Namer and overrides it.

Removing a table should be done with the schema.RemoveTable(string) extension method.
schema.RemoveTable("sysdiagrams");
If you just do schema.Tables.Remove(table), all the foreign key associations still point to the missing table and the code generation will probably just fail. The RemoveTable extension cleans up the foreign keys first, so the code generation will still work cleanly.