In VS2013 is there an easy way to update attributes in your model? - visual-studio-2013

I'm using VS2013 in a MVC 5 app. Created EF6 model using the database first approach which yielded the model as expected. Subsequently I will make changes in the database objects (tables, views, stored procs). When I go into the model to update it, the visible model will get updated. Looking at the Model Browser, I have to manually clean up the artifacts that no longer exist. Am I missing something in my procedure?

When you right click and update the model from the database, it will typically add new pieces automatically, but in my experience it doesn't automatically remove pieces that are no longer there, so you'll need to watch for those and delete them manually.
On the plus side, if you had any special customizations around a field (e.g. enum types), and the field gets renamed, this gives you a chance to compare the two configurations and make sure they line up before you delete the old field from your model.

Related

Symfony/Doctrine: "Soft update" if a certain field has changed

I know you can soft delete in Doctrine (i.e. do not delete a record but rather add a "deleted" value). There's an extension for that.
Now I wonder if there's a way to "soft update" a record. I mean not actually update the record but rather create a new record and make the old one invalid. In the same extension as soft-delete, there's a function loggable, but this one logs to a different table.
I could create a controller that, instead of updating, soft-deletes
(and thus invalidates) the old record, and then creates a new one
with the new values. But I'm unsure if this is a good practice.
Maybe I should create this action on the object itself? But I'm
unsure how to do this.
Edit
I've looked into Versionable and EntityAudit (as suggested by Tomas), but it seems these bundles do way too much. I merely want to check if a given field is different from the old one, and if not: soft-delete the old one (I'm using softDeleteable so a simple remove() will do); then create a new one with the changed values.
So ideally it would lurk in the shadows until an update is performed. Then read from the mapping configuration which fields it needs to watch, and if these fields are indeed different from what's persisted, the program should execute the remove() and persist() commands.
This extension might suit your use case:
simplethings/EntityAudit
It records any changes you want to track.
So it should be pretty easy to modify it to meed your needs.

Saving copy of old table entry to another table when updating table entry with SaveChanges()?

Im working on an online store project where I have already made it possible for an administrator to update different table entries via the store gui (like items, user profiles, orders etc). SaveChanges(); is used to save the changes.
Im currently trying to figure out how to make this work:
An entry in table "items" gets updated.
Before the entry in the table "items" gets updated, a copy of the old entry gets saved into a table named "history-items".
The copy that is saved to "history-items" preferably has a timestamp.
How would I go about doing this? (As you might tell, I just recently picked up visual studio, and am pretty new to everything)
Thank you.
There are atleast 3 ways to do this:
If you are using SQL Server 2008 or newer this is now built in functionality, see: http://msdn.microsoft.com/en-us/library/bb933994.aspx
If you opt not to use that then the simplest solution is to use database triggers.
If you want to do it in C# code, then you need to read the original values before saving, and save these original values to the history table. For reading original values see: How to get original values of an entity in Entity Framework?
I would go for option 1 if possible.

How to debug problems with The Entity Data Model Designer (Entity Framework)

I have inhereted some project which uses Entity Framework in a way which makes it hard to make there any changes. It uses QueryViews for almost all tables (cca 50 tables) and of course stored procedures. Now I have to change there quite a lot of things ... rename tables, add tables, change columns etc.
When I tried to use the "Update Model from database ..." wizard, than after the update (where I added/removed the tables and let refresh the others using the wizard) from the database the Entity Data Model Designer rendering stops working ... there is just blank window with the text "The Entity Data Model Designer is unable to display the file you requested."
So I tried different approaches (like manually editing the edmx file), but the problem remains. The editor shows only the "The Entity Data Model Designer is unable to display the file you requested."
The mapping using QueryViews makes it probably more complicated. It is well known that the designer can not work with the QueryViews properly (one can not edit them using the designer) and the Entity framework engine even does not recognize that the columns from CSDL are mapped using the QueryViews and complains on each and every column (which is mapped using QueryView) that "Error 11009: Property 'XXX' is not mapped." I see exactly 100 errors like this. Maybe somewhere after the 100th error, there is some hint (in the form of other errors) how to fix the issue with Designer, but I don't know how to see them. The 100 limit is most likely hardcoded in VS2010 (http://stackoverflow.com/questions/2880936/how-to-increase-error-limit-in-visual-studio).
Btw. the code (classes for entities etc.) is generated without problems.
So, the question is:Is there a way how to see some log or something, where would be noted why the Entity framework Data Model Designer is not able to render anything?
Or is there at least some way how to see the rest of the errors (besides the 100 errors)?
Or does anybody know the ideal way of dealing with updating schema in EF besides using the wizard?
Try to add new EDMX and right click >> open with >> XML editor, then you can see a complete set in an empty model definition in EDMX. So you can compare the two EDMX and check notice which part of the EDMX is missing.
Here is the error link
In the end I have just do all the changes manually by editing the xml. However, I used the model designer (the GUI integrated in VS for EF) for creating the whole CSDL layer. So my approach was to carefully choose tables in the correct order and add them one by one in the multiple iterations of the following steps:
Use the model designer to create the csdl layer for the chosen table including all relations with already existing table. This at least ensured that the designer was usable later on and it saves the manual writing of the CSDL objetcs.
Write the SSDL layer, which should reflect the DB table.
Write the mapping layer (in my case using the QueryViews).
Try to compile and resolve all compile errors.
Repeat for next table (or more tables if you find it easier).
I hope this will help somebody.

MCV3: View to edit entity has to hold every column?

I got a silly general question...
If I generate a strongly typed view of an entity and chose "edit" as scaffolding, then the view does contain every column for that table. Changing and saving the values via setting it modifierd and call db.SaveChanges() does work in the controller. So far, so good.
But if I remove just one of that columns inside the view, then saving doesn't work anymore.
Is there a rule describing this? Is it only possible to make view with every column when wanting to save the model later on? I don't want to make 90 of 100 columns "hidden"...
PS: When editing a value in another table which is connected via Foreign Key (like customer.address.STREET) saving also does not work. Does everything of the entity ADDRESS has to be inside the view? I really don't get that.
Besides that: If I create my own ViewModel containing two entities: Do they also have to hold every column of both entities? This would be a whole bunch of traffic...
Answer is: You should not use the .Modified state. Instead using the UpdateModel method works fine without every field.

MS CRM 4 - Custom entity with "regardingobjectid" functionality

I've made a custom entity that will work as an data modification audit (any entity modified will trigger creating an instance of this entity). So far I have the plugin working fine (tracking old and new versions of properties changed).
I'd like to also keep track of what entity this is related to. At first I added a N:1 from DataHistory to Task (eg.) and I can indeed link back to the original task (via a "new_tasksid" attribute I added to DataHistory).
The problem is every entity I want to log will need a separate attribute id (and an additional entry in the form!)
Looking at how phone, task, etc utilize a "regardingobjectid", this is what I should do. Unfortunately, when I try to add a "dataobjectid" and map it to eg Task and PhoneCall, it complains (on the second save), that the reference needs to be unique. How does the CRM get around this and can I emulate it?
You could create your generic "dataobjectid" field, but make it a text field and store the guid of the object there. You would lose the native grids for looking at the audit records, and you wouldn't be able to join these entities through advanced find, fetch or query expressions, but if that's not important, then you can whip up an ASPX page that displays the audit logs for that record in whatever format you choose and avoid making new relationships for every entity you want to audit.
CRM has a special lookup type that can lookup to many entity types. That functionality isn't available to us customizers, unfortunately. Your best bet is to add each relationship that could be regarding and hide the lookups that aren't in use for this particular entity.

Resources