Changing field names in edmx file in a smart way - entity-framework

I've just noticed how terribly tough Entity Framework makes simple task of changing name of some field in some table in the model. There are following difficulties. Renaming (or changing type) of field using GUI:
doesn't change the mapping
doesn't rename names in generated models, but regenerates them
So after renaming a field we need to manually update edmx's xml and deal with all references to old name of the generated POCO in all places of our project.
Is there any way to do it smarter? Are there any tools for it?
(I'm using EF 4)

Related

Entity name conflicts in Visual Studio

I'm new to the Entity Framework. I've created a model-first schema in Visual Studio 2012 with EF5, but I'm having trouble getting the generated code to build. One of my entities ("Test") has the same name as the model (edmx) and the project (csproj) files, so it conflicts with the namespace (Test is a namespace but is used like a type). So I decided to change the namespace by setting a "Custom Tool Namespace" for the .tt files. This worked, but then I found that the "Test" entity's generated .cs file was entirely empty (other entities were generating properly), so I had build errors where other entities reference "Test". Renaming the entity results in a properly generated class, and therefore a building project, but I really want to use the original name.
I will probably end up scratching the project and starting over, ensuring to choose unique names for the project, the model, and the entity. But I'd rather know how to fix this if possible in case I run into something similar when the project is further along and it's not so easy to start over.
you can use use an alias on the Using Directive, e.g. using Project = PC.MyCompany.Project; to differentiate between namespaces. see MSDN

Entity Frame work Coded first - Create Readonly EDMX Model

I am just starting out with EF Code-first:
If I create a read-only EDMX file from my code first can I save this readonly file as a stand-alone file and then include the resultant file in my project without affecting code first implementation.
Even though I am doing code first, I like a picture as it helps when I am explaining the db model to others. But I do not want to included this if it effects the code-first approach
will adding the EDMX file to the code-first project cause problems with the code-first approach?
No it doesn't have any effect on your code First approach. You can add the .edmx file directly to your project from Add-> New Item and generate the model from database without affecting your code-first approach. Just remember to use different namespaces so you don't get namespace conflict between your code-first and model classes.
To remove the Model, just remember to delete the related connection string in app.config/web.config to prevent your config files from becoming messy.

Entity framework: Manually maintained mappings

I've been using EF for a while (4 with model first) and so far I've not created any mapping manually. Whenever I need more entities/tables, I add an entity and the associations (all foreign key) and click "update database from model", which, as is well known, doesn't update any database from the model (although it does need a database connection for reasons I don't know). What it does is generating a storage model and the appropriate mappings to it, which are all stored back to the same edmx xml file.
So far, that has always been enough for me but I'm wondering what the workflow would be if one is to tweak the mappings and storage model manually. "Update database from model" overwrites all manual customization - so how is one to fix most of the mappings and storage model? Because I clearly don't want to do it all by hand - in fact I couldn't even figure out how to actually create a table in the storage model other than by editing the edmx in the xml.
I have the same problem. I just use a mixture of methods. If I add a field to the database, I just add the field to the model file. If I do a major restructure, I delete the table and recreate it by generating it from the database. Sometimes, I actually edit the edmx as XML to change or add things. You just kinda gotta figure out what process works best for you. I have managed to avoid heavy customization in the edmx by using the T4 template or changing the database and regenerating.

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).

Entity Framework: Ignore Columns

I have a database that I wish to build an EF model from, however I do not want to include certain columns from the database as the columns concerned are maintained exclusively on the server and should not be manipulated by any application.
Both of the columns are DateTime (if this makes any difference), one of the columns is nullable and is maintained by a trigger on updates and the other is not nullable and set using a default value in the table definition.
I guess I am looking for something like the "Server Generated" option in Linq2Sql; but I cannot find such an option.
Can anybody tell me how to work around this?
Caveat:
I have been trying to introduce business object modelling at my place of work for some years and it has always been rejected because of the amount of additional code that has to be hand-cranked. EF is currently being seen as a viable solution because of the designer and code generation therefore any option that involves hand-cranking the XML will only turn the rest of my colleagues away from EF. I am therefore looking for something that can be done either using the designer or using code.
EDIT:
I guess that what I am looking for here is either...
(a) a way to create the model without EF referencing the columns in the store (ssdl) and therefore not looking to manipulate it in any way
(b) a way to programatically set the "StoreGeneratedPattern" attribute against the property when I create the ObjectContext (the easy answer is to manually manipulate this in the .ssdl, but this would then be overwritten if I refreshed the model from the database and I cannot go down the route where the .csdl, .msl & .ssdl are hand-cranked).
Can you do this with the Entity Framework? Yes; it's easy. Can you do this with the Entity Framework designer? Unfortunately, that is much harder.
The problem you're having is that the column exists in the storage schema (SSDL) in your EDMX. Removing the column with the GUI designer simply removes it from the client schema, not the mapping or the storage schema. However, it's simple enough to go into the EDMX and remove it. Having done that, you can also remove it from the mapping in the client schema portions of the EDMX, and the entity framework will longer complain that it is unmapped.
Problem solved, right?
Well, no. When you use the GUI designer to update the EDMX from the database, the storage schema is thrown away and re-generated. So your column will come back. As far as I know, there is no way to tell the GUI designer to never map a particular column. So you will have to re-do this every time you update with the GUI designer. Fortunately, the EDMX is XML, so you can do this with a XML transform, LINQ, or the XML tool of your choice.
Can you not create a view with the columns you need and import it through entity function wizard and map it to your entities?
You could modify the text template to ignore these columns when generating your entity classes. For example if you added "IGNORE" to the documentation summary, you could modify the template to ignore them by replacing;
Dim simpleProperties as IEnumerable(Of EdmProperty) = typeMapper.GetSimpleProperties(entity)
with;
Dim simpleProperties as IEnumerable(Of EdmProperty) = typeMapper.GetSimpleProperties(entity).Where(Function(p) p.Documentation is nothing orelse p.Documentation.Summary.IndexOf("IGNORE")<0)
Right click on the field in the graphical representation and choose delete. Ive found that sometimes you will get errors when you make a lot of changes to the modeling at once and start to lose track of your changes. Your best bet might be to rebuild the EF generated model.
Keep in mind that when you "update from the database", that old fields on the generated models will not be removed, you will have to remove them manually. For example if you renamed DateField1 to DateField2 in your database, and then you "Update Model from Database", you will now see both DateField1 and DateField2 on the resultant model. This can be a cause of errors.
Do you not want the column to appear in the model at all?
Try selecting the column in the Designer view and hitting the delete key.
Edit
You could make the setter for the property private. Then your app won't be able to modify the value.
Timestamp is a different data type than DateTime. Timestamp seems to be recognized as an attribute the engine manages, much like an identity attribute. You can't "update" a timestamp attribute. Hence, the EDM can manage it correctly (just as it does an identity).
In EDMX designer, select the property and set StoreGeneratedPattern to Computed.