entity framework crash after updating database - entity-framework

I am current running EF 4.3.1 which was recently upgraded from 4.1, using a database first model. I also have my model and objects spread across different assemblies.
As I'm developing however, I'm finding the entity framework almost impossible to live with, because despite having the option "update model from database", seldom can I do so without causing the designer to fire up errors.
If I add an index to a table, create a fk relationship, there's a good chance that the designer will show a 'object null reference exception' when I try do do anything with the database afterwards. To make it even harder, the error message is exactly that vague, not pointing to what it actually could be.
I have asked a question elsewhere and the response I got back was to recreate the edmx. Surely not when I have 60 to 80 entities?
I can't believe that I'm the only one with the issue, and i can't believe any developer could live with the situation, but I can't find information anywhere as to what the issue is - if in fact its not 'by design'.
Can anyone help?

The reason for the crash is due to the de-sync'd relationship between the edmx and the dbcontext.
When changes, or even refresh is applied to the edmx you MUST run the custom tool update before exiting the project. Failure to do this will result in a corrupt edmx., which I have yet not managed to fix without restoring/recreating it.
Whilst this is in your hands most of the time, obviously there are occasions when this is outside of your control and should be something managed by the framework rather than depend on your ability to remember and the will of god. Hopefully longer term this will be included in the framework.

Related

No relationships when generating Entities from Advantage database

I am trying to add tables from an existing Advantage Database (recently upgraded from v8 to v10) to a .NET project via the Entity Framework. However, no matter what I do, the relationships between the tables are not being imported from the database. Obviously, I can just recreate them in Visual Studio, but I'd prefer to keep the data structure primarily in the database.
This link describes the problem, but the solution (permissions issues) doesn't work for me. We're using the IGNORERIGHTS security mode in connections, and user permissions are not enabled on the database.
Based on this chapter, I've made sure that the tables in question are ADT tables, and have Primary keys which are non-nullable. I've also verified that the RI constraints exist and show up in Advantage's visual designer.
Despite all that, when I "Update Model from Database" in the EDMX and select the tables which have a reference between them, no reference is created.
Is there anything else I can try? Some step I'm missing? A setting that needs to change?
I ended up deleting the EDMX for entirely unrelated reasons, and when I recreated it, relationships worked just fine. It's quite possible that Mark's suggestion was the answer, but for new ones only. I didn't do any extensive testing to try and confirm that, though.

Code First model and deployment of new versions of the software

I'm looking on Entity Framework at the moment and working with Code First example. So far I can see that the framework does not handle model changes easily: whenever I want to add another field to a class/table, framework drops the entire database and creates it from the scratch.
Similar behaviour I saw in (N)Hibernate. (I could be wrong here, it was long time ago)
That is ok, as long as I work on tutorial. When a real-life project is involved, you can't afford to drop a database every time you need a new field in a table.
Just imagine scenario, you are working on a project with many clients. Every client has their own database. In release 1.0.1 I need to add a new field to one of the tables. If I drop database in my dev environment - not a big deal. (Still, I need to run a script to populate test data every time DB is dropped, and sometimes even this is no viable)
But what do I do when I need to deploy this new version? Make a SQL script to update client's databases without dropping them? then deploy binaries?
But how is this better than doing database mods separate from code changes?
(sorry for my bad english)
This is exactly why Code First Migrations exists. Take a look here (automatic migrations) and here (code-based migrations)

Pre-Generate Views for EF Code First

Having finally deployed to live a new site built with Entity Framework Code First and MySql, one nagging annoyance is the pause often experienced while view metadata is cached. This seems to happen far more frequently than the application pool is recycled. (?)
I guess that for a site which has a constant stream of traffic this might not be noticed often. For a new site with infrequent traffic it feels like every time I go to the site there's a 4-5 second delay in rendering the first page.
So I tracked down the following links:
http://msdn.microsoft.com/en-us/library/bb896240.aspx
http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
The EF Power Tools CTP1 looked promising, apart from the fact that it seems to be broken for EF 4.3 and hasn't been updated for months. Also I'm not sure how that tool would fit in to our automated build process.
The OptimizeContext() or WriteEdmx() methods that EF Power Tools is using throw this error
System.Reflection.TargetInvocationException:
Exception has been thrown by the target of an invocation. ---> System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string. ---> System.Data.SqlClient.SqlException:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I initially assumed this might be MySql related but several others on the EF Power Tools Q&A section are reporting it.
So, in short, how do I pre-generate views for EF 4.3 code first, preferable in a way that can slot into an automated build process?
Pawel answered my related question:
Entity Framework initialization is SLOW -- what can I do to bootstrap it faster?
by building T4 templates:
http://blog.3d-logic.com/2012/05/28/entity-framework-code-first-and-pre-generated-views/
which also aren't perfect, but more usable than anything else that I've seen.
You could then do something like:
http://www.platinumbay.com/blogs/dotneticated/archive/2009/08/28/running-t4-templates-with-msbuild.aspx
to add it to the build process, but it's sort of slow and so you might want to just leave it as a(nother) manual step in updating the EF model.
Really, they need a warning that says, DON'T USE EF WITH > 500 TABLES, it becomes increasingly annoying to use.
I have encountered this same error when I am running VS2010, EF v4.3.1 and Entity Framework Power Tools Beta 2. The solution for me was to run Visual Studio as admin. (Done by right clicking the Icon and select "run as administrator" from the context menu.) At that point I was able to use the "generate views" option in EFPT.

Core Data - Add object to a relationship without firing a fault

When I try to add a new ManagedObject to an existing relationship of an entity using the Core Data generated method "addArticleObject", a fault is fired for the articles relationship. Is it possible to add an object to a relationship without causing a fault to fire for the existing objects in the relationship?
I'm not sure I'm understanding your question exactly, but if you modify your core data model, as long as you go on the simulator and delete your app, then rerun it (so it regenerates everything), then you should be fine. You also might have to delete and recreate your NSManagedObject classes, but that's a quick thing also.
If you've altered the structure of your Core Data model, unless you've done so with a migration you'll need to clear the previous version from the Simulator and any handsets you're testing on. The error which occurs if you don't isn't particularly helpful.
Use the 'Reset contents & settings' option in the Simulator, and delete and reinstall the app on a phone. Simply rerunning the app isn't enough.
It seems that it is not possible to add objects to a many-to-many inverse relationship in Core Data without having both objects in the relationship in memory. I'm sure there must be a good reason for that requirement, which I would like to know. This has however caused me to have to refactor the database and all associated code

Entity Framework 4.0 'Code First' approach

I've been working Entity Framework trying to get better with it. I'm liking what I'm seeing thus far but now have a question. With this new 'Code First' approach (from the CTP 4 download) we can now use EF from a code first approach, but I'm trying to find out if one can use an existing EDMX file with this approach.
I have a project I'm working on which has an EDMX file and I notice the ModelBuilder has a RegisterEdmx method but am not finding a lot out there on whether this will allow to use an existing EDMX file with my code first approach.
Also, I know with this new CTP things like RecreateDatabaseIfModelChanges are avilable but these options drop the database and recreate it, wont this cause all your data to be lost if you ever change your models? Is there something I'm missing here?
I can't speak to using previously generated EDMX files but there is support to use Code First with existing databases. As for the automatic Recreate, yes, this will kill all your data. This is meant only in rapid development where the persistence of data doesn't matter (and, in fact, is likely unwanted as you discover issues with business logic and want a clean start with your updates.)
This is meant only as a quick way to develop. As of (when Scott Guthrie blogged about the CTP - jump to section 5) there are no data migration features available. Your options are to manually update the database to match your model, delete the database and let it be recreated or set the automatic recreate option. Only the first option is non-destructive to your data.