Entity framework pre-generating views - entity-framework

I am pre-generating views for the entity data model in my project; following the instruction given by MSDN:
http://msdn.microsoft.com/en-us/library/bb896240.aspx
I have completed the changes without any problem. I can now see CSDL, SSDL and MSL files added to my project. Does this mean I will have to update these files everytime I update my entity data model? If so, its quite a bit of overhead since our database changes quite frequently.
Is there a better way to update these files everytime I update the edmx?
Thanks

You can do what we do in our project:
Open the EDMX and right click the surface-> Properties.
Set Metadata Artifcat Processing to "Copy to Output Directory"
Add the resulting /bin/debug/ .csdl .msl and .ssdl to your project (show all files in solution explorer and right click, Include in Project)
Next set the .csdl .msl and .ssdl properties to be embedded resource.
Build and reference the files in the entity framework connection string in your runtime project.

Related

Update model from database not updating metadata

My project has stopped updating the metadata when I add new tables or stored procedures. Its on Visual Studio 2017. with .net framework 4.6.1
I use the Update model from database wizard and after selecting the new item and clicking finish. If I check in the model browser i can see the new classes in the Model and in the model.store. Also, Its in the solution explorer .edmx.
The problem is not all projects can reference the new items and i noticed the new items are not in the Entities Metadata.
Things i tried
Run custom tools on .tt files
clean all projects individually and rebuilt
closed application and open it
Please help
I have the entity framework (DB) in a separate project the is linked in the references. I found the only thing that worked for me was to unlink the reference, rebuild and have it fail and re link the project. SOOOO frustrating as this functionality used to work quite well.

To modify or not to modify the EDMX file

Is it generally a good idea to manually modify the generated EDMX file when working on a .NET project? I had a project once where the developers were modifying the edmx file manually, and it was accepted as a rule to never regenerate the whole EDMX file. For example, one change we made was with some fields whereby we changed them to enum values.
Then I was hired on a project where the developers were telling me to regenerate the EDMX file whenever I made database changes. This was a totally different point of view.
So what is the better solution? Has anyone had any nightmares where one was better than the other.
A general rule is not to edit any auto generated file whether it's EDMX or WinForms designer. However, if there is absolutely no other way and you do need to edit auto-generated class, then do it by using partial class. This way your change won't get lost after the class is regenerated.

Visual Studio 2013 and Entity Framework

I'm using VS 2013 and SQL Svr 2012 and trying use EF to populate a gridview. As this is a very simple test, I have just one table with a key and a couple of data fields. The process of creating the model seems to work fine - it shows as expected in the designer view, the files are created in app_code, a bin folder is created with 2 dlls, and the config file is updated with a connection string and other entries.
When I try to select the "named connection" I get the error "Unable to load the specified metadata resource" and it talks about rebuilding the project to get the assemblies.
I've spent hours reading other cases like this and I can't understand:
Why this is happening when it was never a problem with VS 2010
How do I fix the connection string to point to the resources it wants. I'm totally lost with all the technical advice about fully qualified assembly names - nothing I try works.
How do I set some parameter to force the resources to be in the proper place? All the advice I've read doesn't seem to relate very well with VS 2013
I need to decide if I should just uninstall VS 2013 and use VS 2012. Currently, I'm using VS 2010 with ASP 4.0 and I'd like to move to ASP 4.5 and generally keep up with Microsoft technology. However, I don't have the skill level to deal with a VS release that is buggy. It seems to me that this EF execise could be due to a either a bug or some odd corruption on my computer - I'm trying to figure out which is the case. I installed VS 2013 Express and SQL Svr 2012 on a clean machine that had none of these tools in the past.
I'd really like to see someone who is very familiar with Visual Studio try to replicate my problem as it only takes a few minutes to do so:
In MS SQL Server, create a tiny DB with one table that has a primary key and a couple of data fields and populate with some test data - Call it TinyDB with TestTable1.
Using VS 2013 Express for Web, create an "New Web Site" with the option for "ASP Empty Web Site" using VB.
Add an ASP App_Code Folder containing an "ADO.Net Entity Data Model" and leave the name as "Model".
Generate the model from the DB and make a "New Connection" for "TinyDB" and using Entity Framework 6.0 and then select "TestTable1". Note the namespace is "TinyDBModel". Clicking "Finish" will generate several items in App_Code, other folders and also update web.config.
Add a web form called default.aspx and drag a gridview onto it. In design mode for the gridview, select "new data source" and then the "Entity" option (note name of EntityDataSource1 which appears in default.aspx). Click next and under "named connection" select "TinyDBEntities" - this is where mine gets the error "Unable to load the specified metadata resource".
Note that this exact exercise in VS 2010 works perfectly and produces a grid with the correct data displayed.
Unfortunately, the EntityDataSource control does not work with the latest version of Entity Framework, EF6. Hopefully the control gets an update in a future update version of VS or EF, but for now you can't use the two together.
There are a few solutions / alternatives:
Drop back to EF 5. In order to do this, delete the .edmx and .tt files from the App_Code folder, delete the EF assemblies from the Bin folder, and remove the references to EF from web.config. Then rebuild the site, add a new ADO.NET Entity Model and choose version 5 in the Choose Your Version dialog. Then delete the .tt files from the App_Code folder, open the EDMX diagam, right-click an empty spot and on the Properties Grid set Code Generation Strategy to Legacy ObjectContext. Then rebuild again, and it should work.
Use Model Binding. This is really the recommended solution. The Microsoft / EF team recommends people to stay away from the EntityDataSource control and only use it for Dynamic Data web sites, Model Binding is the preferred solultion. It's pretty clean and straight forward to use, and gives you a lot of control over your queries and object updates. For more info about Model Binding, check out: http://www.asp.net/web-forms/tutorials/data-access/model-binding/retrieving-data

EF 4.1 POCO template generator conflicting with ClassObjects.Context.cs

I've been using EF 4.1 and the POCO template generator.
I love and hate EF. I love the time I save. I hate maintaining the EDMX file.
But while keeping the EDMX in sync with the database has been a challenge, I'm now overwhelmed by the POCO generator. Up until now, the POCO generator has created POCO's for me and kept the ClassObjects.Context.cs empty and non-conflicting.
After my most recent refresh I have had constant problems with "Amiguity between 'perseus.DataLayer.accounts' and 'perseus.DataLayer.accounts'.
I get this error for every POCO.
I'm on the edge of panicking as I recommended and owned the maintenance of EF. I've spent a couple days on trying everything from database refreshes to deleting all items from the EDMX file and reloading them from the database.
Nothing has made a difference. I have no clue what has suddenly changed from the last few months of relative stability. I'm seriously lost as to what I can do from here.
It looks to me like you have not disabled the code generation strategy. click on the designer background and check the properties tab, the first item is Code Generation Strategy. This should say None, and not Default.
Editing the edmx manually is difficult and error prone. I would suggest copying what you have off to another location then letting the tool regenerate the edmx for you. Compare the two.
I worked with some DB2 entities that I had to manually sync and had constant issues. The tool would clobber my changes and I would have to manually replace that with working code.
If at all possible, don't modify the edmx code yourself. If you must, make the changes, copy those off to a text file in the project for tracking/safe keeping.

EntityFramework gives IDisposable error

I use EF for the back-end DataLayer in my asp.net websites. I create a class library and add the Model in it, reference it and use it from the ASP.NET Website. But this time, I generated the model from database but it seems does not implement IDisposable, and the methods I used to see are not there. DeleteObject , SaveChanges etc. There are only Two tables in the DB and the Model was generated with only the TableSets and two methods AddToTableSet ... Had anybody encountered such a problem?
alt text http://techlipse.net/EF-problem.jpg
Did you extend the generated ObjectContext and then perhaps rename it in the EDMX? You might be looking at your part of the partial class here without the generated part.
What does the Properties panel say for your EDMX for "Entity Container Name"?
When you right click on Entities and go to definition do you get to the correct generated class?
And lastly, but probably most likely, did you perhaps delete the Reference to System.Data.Entity from the project or not reference it in your web project?
Is you web project even a Web Application project???
I have my Entity Framework code in its own project, and was referencing it from a separate project. NuGet sets EntityFramework only on the EF project, which is how the reference gets automatically configured to the latest version. To fix this error:
Tools -> Library Package Manager -> Manage Nuget Packages for Solution
Select Installed Packages and find Entity Framework
Click the Manage Button, and check all the project that will be accessing the EF Data Model.