How to update entity objects without deleting? - asp.net

I have an entity generated from my database that contains one table. When i make changes in the DB i obviously would like these changes to reflected in my model.
My current approach is to delete the item from the designer and then right-click - update model from database. This works for me at the moment. Is there a different approach to refreshing these entity tables ?

Why are you deleting them? You can simply right click on your model and select Update Model From Database... and you are done.

Related

A few questions regarding importing a manually created data entity

I used the data entity creation wizard and selected Reqplan table as the main data source, then I manually added ReqPlanVersion, ReqPO, ReqTrans table as additional data sources and created the relationships below.
As for the data entities fields I manually dragged a subsets of fields from the three manually added tables.
However when I try to import the data and add file, I receive the following issue:
Q1. In the past for some other entities I have changed ‘Allow Edit on Create’ from ‘Auto’ to ‘YES’ on these fields and it has worked but I am not sure if it’s the only way or is it following best practice? Also what is the determining factor for a field to be editable or not during import since they are all on AUTO?
When I try to map source to staging manually by drawing the mapping lines I get below issue:
Q2. What is going on with the configuration key? Is it because I manually dragged the fields from the additional data sources but not using the data entity creation wizard?
Lastly I been getting below issue:
Q3: Is there a way to find out which unique key it is referring to? Is it talking about the EntityKey in my Data Entity or Indexes in staging table? In either case there are more than one so I am not sure what it is referring to?
Thanks in advance.
Response from the community forum:
1) Check allowEdit property on table itself, so if it is "No" there then auto means "No". If you want to update them through data entity you will have to force them to "Yes"
2) It's not connected to manual addition, it just says that tables used in the entity has configuration key disabled, so you cannot export or import data into them, however, these tables could be added by wizard or manually, it does not matter. Also, Configuration key could be on fields as well or on EDT that these fields use, check them as well.
3) Entity has Key node and there and there you have key generated by wizard for you. It is used by framework to understand if record should be updated or created, if it does not work for you, change it on the data entity and regenerate staging. You need to refresh staging because error you get is SQL error, at this stage SSIS transfers data from a file into a staging table and data could not be copied because of index vialotion, so check staging table index and see if your file has any duplicates.

Why Updating From Database Does Not Bring All Tables from the Database?

When I update the model from database, I am not able to access to some of the tables as entity.
Through Model Browser, I can see those tables under MyDBEntities.Store (I don't know what it is) but under the Entity Types, these tables are not listed. Tried to downgrade to EF5, tried opening and updating the model with VS2012. Tried deleting and recreating the model so many times but no luck. If I right click on Entity Folder and select Add New Entity, under the Base Types, these tables are not listed either. All tables have PK and FK by the way. Anyone has a clue?
EDIT:
I just noticed, if remove the model and while updating it from database, if only add these 5 tables, they are being added properly. But this time caused the run-time error: Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
When you add an association table that creates M:N relation between two other tables and has no additional attributes, then the association table is not shown in the model.
See http://smehrozalam.wordpress.com/2010/06/29/entity-framework-queries-involving-many-to-many-relationship-tables/.

Razor MVC - Object Edit Field Change Log

Users modify a DB object in an edit form that I have, pretty straight forward.
I need to implement a 'change log' on this object. I need to record which fields where changed and what they were before and after. I'm using Razor MVC.
I've done this by writing triggers for the table on update/delete. On update/delete of a record, the trigger pushes the record to a History table, in a History database. This creates the change log. Then you would just need to display it; to identify the change would require evaluating each and every field.
There's nothing already built that wold do this for you that I know of.

Entity Framework update issue

I am not sure if it's a bug, but when i add a new view or a new stored procedure to the model it updates all the tables that exist. So my question is should it work like this and if it should how can i add some new procedure without updating the whole model?
Yes, this is the correct functionality when using the "Update Model" function for EntityFramework. It looks at every database object and updates the EF Model to match what it finds in the database. This is, in part, because the designer does not let you specifically choose which tables or view to update, so it verifies any changes in the database. This allows the model to proactively ensure that when it connects to the database there won't be an error based on the database changing.

MVC3 check for references to model record before deletion

What is the best practice for checking if any references to a particular model record exist before deleting that record? Basically, I have a model that represents images, and all metadata associated with an image. Other models will have references to one or more images (depending on the model).
Let's say for example, I have an "Item" which has a "MainImage" and an "AltImage", both of which are just references to the Image model. If I delete an Item record, I have to check if the two images are referenced by any other Item, or any other table, and if not then delete the Image.
How would I go about this?
Since you are using a database, let it maintain referential integrity using foreign key constraints on the images. The database or EF will prevent you from deleting the image if it is still being referenced. You can catch this exception and continue processing the request without deleting the image.
I found a blog posting Inferring Foreign Key Constraints in EF that may be of use in setting these up.
Couple ideas based on what you prefer:
Create trigger in you DB
That will delete alt img.
and everytime you want to delete the main record row the other record will be deleted too.
Another (based on nhibernate)
Make sure that alternative image in your entity framework might have possibility to cascade commands. And in that case if you delete one image the other one will be deleted too. Here is one example from google how to do this
Most clumsy but easiest is:
Delete both records when you deleting the image.

Resources