Entity Framework Database first from non-English DB - entity-framework

I'd like to use DB first Entity Framework, but my source DB is not in English.
Is there a way to intercept the DbContext models generation, and provide a map for some/all table and column names (other language column name -> English column name)?
I'm not sure if there's a possibility to use T4 templates for this.

It seems there was another approach available, and it seemed more suited for me, so I took it.
The main edmx file of EF turns out to be just an XML document, so I created a CSV map file between old/non-English table/column names and created a small nodejs script to replace all occurrences of:
<Property Name="OLD_NAME"
<ScalarProperty Name="OLD_NAME"
<PropertyRef Name="OLD_NAME"
..with new/model names. I applied it on edmx model and mappings sections only, and though not a complete solution it took me 95% there. I still have to rename NavigationProperties, but I'll do it in the edmx designer manually since there aren't many of those.

Related

Stop Entity Framework from updating edmx model with a column that isn't needed

I have rowguids in all my tables to help with change tracking in all my tables. I don't want/need these tables in my edmx or my entities. However, I do still need to make changes to other things sometimes so everytime i go to update model from database in the edmx it adds all the rowguids in all my tables everytime and i have to manually delete each one. Is there a way to handle this from happening? Is there a way I can maybe edit the T4 to maybe ignore that 'rowguid' column?
Database first Entity framework
I don't believe there is a way to intrinsically tell the edmx to not pull certain columns. Because you want to prevent it in both the entities AND the edmx, altering the T4 template will not help you prevent it from being added to the edmx.
If you change your mind you could certainly prevent entities with a certain naming convention from being added in the T4. I prefer to keep my edmx files clean, and manually deleting is the only way I know to remove unwanted columns.
For others who may be interested in understanding EF T4 here's a basic article that I've found helpful in better understanding what it does and how it works:
http://msdn.microsoft.com/en-us/data/gg558520.aspx
To your question about generating a scrub script: edmx files behind the scenes are just xml markup trees. Every element shown in the viewer corresponds to one or more element in the tree. Here is a simple example of what the xml might look like for a simple Program entity.
If you wanted to scrub specific columns you could remove them from the underlying xml.
<EntityType Name="Program">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ProgramName" Type="varchar" MaxLength="150" />
<Property Name="DateTimeCreated" Type="datetime" />
</EntityType>
Sadly, this is not all you would also need to edit there are two locations in the edmx this and the conceptual model. you might also have to remove associations if they reference the deleted property.
You will also need to edit the msl. I was hoping to mock up a personal example, but in lieu of that I have found two articles that discuss or examine the process others have gone through for this.
http://social.msdn.microsoft.com/Forums/en-US/e61192da-8c51-4f17-8745-c4455c836f9d/ef-modify-csdl-ssdl-and-mapping-in-code-and-then-save-in-the-csdl-ssdl-files?forum=adodotnetentityframework
http://blogs.msdn.com/b/dsimmons/archive/2007/12/07/how-to-use-your-existing-csdl-msl-ssdl-files-in-the-entity-designer-ctp2.aspx
These two might give you a better idea of what's involved in that process. It might still be worth it for you if it's a severe use case.
If there are tables you do not want in your .edmx or entities, then don't select them when you create the entity data models.
When creating the entity data model in the wizard, go for "EF Designer from Database" (database first).
To get there, right click the solution -> Add -> New Item... -> Under the 'Data' category, select a new ADO.NET Entity Data Model -> 'EF Designer from Database'
After the connection settings prompt, make sure you only include the tables you want.

Cannot select table for discriminator (model-first)

I'm trying to kick off a project needing table-per-hierachy. I've done this in the past with NHibernate, but we want to avoid having to hand edit XML mapping files - so are trying to use Entity Framework and it's Designer.
I've been following this example online:
http://mosesofegypt.net/post/Inheritance-and-Associations-with-Entity-Framework-Part-1.aspx
When trying to select a Table from the "<Add Table or View>" dropdown, I can't select any tables, instead the list shows "(Empty)".
The only deviation I've made from the tutorial is that I didn't generate the Person table off the sample database, as far as I can tell this shouldn't make a difference (we want to generate the DB off the model, not vice versa).
Not sure what criteria a table needs to meet to be eligible to add a discriminator to...
If you need to generate DB from the model you cannot see any table in mapping because both mapping and information about tables is generated when you generate the database. EDMX has quite complex structure (much more complex than very easy hbm files for NHibernate). The complexity is even worse because EDMX doesn't have anything implicit - everything must be described and it must be described three times.
EDMX consists of three parts:
CSDL - conceptual model you see in the designer (classes)
SSDL - database description - this can be browsed in model browser but it is read-only
MSL - mapping between SSDL and MSL (that is what you are trying to edit in that window).
When you are going to use model-first (draw entities in designer and generate database) you define only CSDL and everything else is generated with SQL for the database. You will also probably need another template / workflow for DB generation because I guess it will by default use Table-per-type inheritance. Check Database generation power pack - it should contain template for TPH.

Entity Framework model-first design not won't let you edit the table mappings?

If we've been using an Entity Framework 4 model for some time, and we eventually want to switch the underlying database to a different vendor's product (say, from SQL Server to MySQL), is it simple to adjust the table and column mappings in the entity model without needing to update any of the entity class code?
We're trying to design code that is as database agnostic as possible, so I'd like to know in advance how much trouble we're in for if we ever switch our databases around. Ideally, we'd like to not have to touch our applications that use our entity classes. I can't seem to find any way in the entity designer or XML editor to adjust the underlying database column names without it giving me an error.
(I can, however, edit the entity's property names in the designer while leaving the database column names alone, but that's the opposite of what I need.)
Thanks!
EDMX is not database agnostic. SSDL part of EDMX is tightly coupled with database server (in case of MSSQL even with its version). You need separate SSDL for each supported database server.
I don't understand how changing column names relates to database agnostic model. Reverse is true! If you need your database to have different column names for different server products you need separate mapping for each of them!
Changing column names when using model first is possible only if you modify T4 template used for generating database creation SQL script. But every time you create that script designer will delete whole your storage description (SSDL) and mapping (MSL) and replace them with a new one.
The easiest way to have database agnostic code is using code first but even then you can have problems with some type and feature inconsistency among servers.
If you want database agnostic ORM you should probably check NHibernate.

Global rename identity for enity

All identities of entities in model have name "EntityNameId". How I can rename all identity to "Id"?
Two way I can think of
manually in EF designer
manually editing EDMX XML file by using some regular expression replace
The first one is safe, the second one is tricky because you only have to rename some of them. SSDL should stay as it is and mapping should only rename entity IDs.
If you have something like up to 50 entities, I suggest you rename them in designer manually. It's safe and it shouldn't take too much time (unless you've written a lot of EF code that uses these already).

How can I make the Entity data model designer use my database column descriptions?

I am using EF4 with Visual Studio 2010. I have a SQL database already created and all my columns are documented with the Description property in SQL management studio. When I create the edmx in Visual Studio, how can I make it read those descriptions and use them to add comments to my generated classes? I know I can use the Summary and LongDescription in the edmx properties but I'm looking for a way to do this without having to copy and paste every description.
Thanks!
There is a feature request for this. Feel free to add your votes to help make this available in the future:
Retrieve the SQL Descriptions in Entity-Framework feature request
Huagati has some great tools for working with EF and L2S. One of the features is updating the EF documentation based on the SQL database:
Huagati website
From the website: Update ADO.NET Entity Data Model documentation from database retrieves free-text table and column descriptions, and index definitions from the database and updates the xml documentation fields in the EDMX designer with the descriptions.
It seems they look for these fields in the database and then update the model XML directly. Probably someone could create a VS Add-In that would do the same without the price if this is the only feature you wanted. I'll add this to my list of "future" projects (though I never seem to find time for these!).
Hope that helps!
I've been looking at ways of hacking something together to populate the edmx with the meta data from the database.
The summary and long description edmx properties are stored in elements under the EntityType element.
<EntityType Name="EntityName">
<!-- Without this element classes are typically generated with
"No Metadata Documentation available." -->
<Documentation>
<Summary>Entity Summary</Summary>
<LongDescription>Entity Long Description</LongDescription>
</Documentation>
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="String" Nullable="false" MaxLength="25" Unicode="false" FixedLength="true" />
<!-- Lots of other properties -->
</EntityType>
The relevant section of the edmx file, the Store Schema Definition Language (SSDL), is created by System.Data.Entity.Design.EntityStoreSchemaGenerator.GenerateStoreMetadata(). Or at least this is the case with EdmGen.
I'm going to see if the EntityStoreSchemaGenerator.StoreItemCollection can be modified before being used by EntityStoreSchemaGenerator.WriteStoreSchema(...) to output the XML.
Update
Well that was annoying. System.Data.Metadata.Edm.Documentation is sealed and only has an internal constructor. Both properties of interest can only be set internally as well.
So it seems like a dead end approach.
I don't know that the designer itself has any extensibility points. However, once the Summary and LongDescription fields are populated in your edmx file, those value will remain there, even if you make other changes or re-updated your model from the database. Unless you delete a table and re-add it, those values will remain populated.
So you could either just copy and paste them all in one at a time (how many tables are in your model? This goes quicker than you think), or write a program to extract the info from your database (using SQL SMO or something), and have that program edit your edmx file, populating the Summary and LongDescription fields (make a backup of your edmx each time you try your program -- you don't want to botch your edmx file and have to start over).
If you have large models, and you're making lots of them, writing a program to do it automatically is worth your time. If you've only got a few models, with not too many tables, copy paste it is.
You might want to think about submitting feedback to the Entity Framework team here. Seems like the designer should automatically pick up on the description field from SQL Server. Would make a good feature request.