Solved

How should I handle objects that Entity Framework doesn't want to update?

Posted on 2015-02-24
5
122 Views
Last Modified: 2015-03-29
I have a few related newbie questions about Entity Framework. (I'm using Entity Framework 4.0.0.0, according to Web.config, and using a "database first" approach).

1) If I change an object in the database (e.g., rename a column in a table or change the output columns of a stored procedure), and then in Visual Studio I select "Update model from database" and "Refresh," the object changes still aren't reflected in Entity Framework.  (Unlike with the "Add" tab, the "Refresh" tab doesn't let one select what to refresh, so it's hard to know if it's doing anything when I click Finish, plus the code never works if I rely on the "Refresh.")  So my workaround is to create the same object with a different name.  E.g., if I had a database object called "item," then I simply create a clone of it in SQL Server named "item1," and then I can add it to EF. That works.  So my question is, how do I get EF to work without renaming my database objects every time I want to change them?  I don't want to be stuck with so many versions of the same objects.

2) Why are "Result" objects (e.g., the results of stored procedures) not even shown in the diagram?  It seems that the only way to update these, if I make changes in the database, is to open edmx's XML and edit that manually.  Is there a better way?

3) Since EF stopped writing to Context.cs (even for new objects), I started writing entries directly there. I also noticed that running "custom tool" overwrites the other cs files, but not Context.cs, which is kind of good, because I was able to do things I don't think EF would have been able to create automatically.  But seeing that Context.cs is *supposed* to be auto-generated (even though it isn't in my case), I'm wondering if editing it is bad practice.  Should I be defining the same partial class in a separate file and then put any custom entries there?  That leads to the following issues though:

3a) Does it matter where I put such a file?
3b) If auto-updating of EF stuff *did* start to work again, wouldn't my customized entries duplicate ones that are auto-generated?  Or is EF smart enough not to create duplicate definitions?
3c) Or is it better practice to just use regular ADO.NET for anything slightly complicated?

4) Overall, what is the best way to "clean up" Entity Framework and have it just work "normally"? And, apart from avoiding manual changes (which I wasn't even doing at first), what is the best way to prevent it from getting into a weird state in the first place (e.g., weird state = a state where one has to run "custom tool" to have it create any classes, and where it doesn't put anything into context.cs)?
0
Comment
Question by:Newbis
  • 4
5 Comments
 
LVL 10

Expert Comment

by:Najam Uddin
ID: 40629353
Do you hit save after refersh? If still does not work, you can manually remove and add things.
To remove,
a. delete object from <DBName>_DataModel.edmx -->  <DBName>_DataModel.Context.tt --> <DBName>_DataModel.Context.cs --> *Entities --> Find your object and delete it
b.  <DBName>EntitiesPartialClass.cs -> *Entities --> Find your object and delete it

Once you do this you issues should be fixed.
0
 

Author Comment

by:Newbis
ID: 40629420
Thanks...I did figure out that I have to keep hitting save in between, but that didn't help me with the specific updating problems mentioned above.  I'll try your suggestion about manually deleting "bad" objects and see if it helps.
0
 

Author Comment

by:Newbis
ID: 40634108
Anyhow, deleting in all those different places, so I can re-add something is still just a workaround...and it doesn't explain why updating doesn't work...what, is the update tab just there for decoration?

And now I notice that if I change a table structure and make all the changes in EF to fit the change in the table, adding a row through EF fails silently!  And I have SQL Profiler open (which seems to be the only practical way to know what EF is doing, as using reflection seems overkill just to see what query was generated), and EF sends nothing...nothing at all!  So if one adds and removes a column, and updates everything in EF accordingly, the linkage to that table is lost forever.

So the only way, really, to do any add operations in a way that can still work if one changes the table structure  is to have a stored procedure for every table, just to do an insert operation.

I never realized that EF is so batty!
0
 

Accepted Solution

by:
Newbis earned 0 total points
ID: 40685246
So the solution to this problem basically is to "run custom tool" on the various locations where that option exists, several times, in between doing a "save all" (ctrl-shift-S), and to create a separate file outside EF for any custom or manually-created methods, using the same namespace and partial class, and giving a different name to those methods than what EF would create.
0
 

Author Closing Comment

by:Newbis
ID: 40694340
Never got a clear answer to why EF behaves like this, but at least I've worked around it.  The worst thing you can do though is to delete any files in EF if you're connected to Team Foundation Server.  ...because then, EF will recreate those files, but they can never be checked in again.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now