Visual Studio 2013 and Entity Framework - 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

Related

EntityFramework Codefirst Updating from Database

I know this isn't the designed method of using EntityFramework but I have a much easier time designing the database in SQL Management Studio to create indexes, link foreign keys, etc... all visually. But I really want to use the automatic database updates for the future when deploying updates to customers.
I designed the original database in SQL Management Studio then created the EntityFramework code first from that database. But if I want to add another table so far, I end up dropping everything then regenerating the DB from SQL Management Studio. This has been ok for the first part of my framework development but now things are starting to get a little more complex.
I've tried to learn the code first mechanism but some of the more complex items have bit me and I don't have a lot of time to allot for this. I was hoping there was a hybrid way of designing in SQL Management Studio then utilizing the database deployment functionality built into EntityFramework.
I'm working on getting the POCO reverse generator running here but having some issues. Ultimately, I think that is the way I'll want to go. But I did find a temporary workaround with existing tools built into VS2019 until I get that tool working.
I made a copy of the name of the model because I had a custom initializing for the connection string.
Delete the model file
I right clicked the folder where the codefirst files are placed in my solution then add-new item
Select ADO.NET Entity Data Model and named id the same as the one in step 1
Select Code First from Database
Deselect saving connection string (it won't overwrite and the needed one is already in the app.config)
Select the tables to import
In my case I deselected the Pluralize object names
Finish
Update the master model file with the custom initializer
In the package manager console enter: add-migration (I'm assuming you already have migrations set up)
name the migration
add-migration [name in step 12] (this applies the migration)
It might seem like a lot of steps, but it's actually not. And so far with a couple of days of playing around with it, I'm not running into any issues.

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.

Migrating TFS 2012 to VSO - Template Issues

I am attempting to migrate from an on-premise installation of TFS 2012 to Visual Studio Online. I have a handful of projects that have been in existence since TFS 2005 and have been upgraded/migrated up to 2012. Only one of those projects has any significant work item activity that I really need to maintain.
That project is using the MSF for Agile Software Development 6.2 template. I have never made any customizations to the template to my knowledge over the years. However, since it has been upgraded / migrated I am not surprised that the template is causing the OpsHub utility issues.
For every Work Item Type in the project, the fields 'Area Id' and 'Iteration Id' show as red errors on the validation screen of the tool for this specific project.
I have already tried the solutions suggested here, but it seems to have made no difference whatsoever. The template on VSO is MSF for Agile Development 2013.3. After looking at the WIT definition files in that template I can see that there is no 'Area Id' or 'Iteration Id' fields (or 'AreaID' or 'IterationId' for that matter) defined. However, in the 6.2 templates that field is specifically defined.
I am sure this has something to do with those being System defined fields and no longer needing to be defined in the individual work item templates or something of that nature. The fields cannot be deleted because they are System fields.
In any case, I have not been able to find a solution to this problem so that I can complete running the OpsHub tool to migrate this main project over. Any help would be greatly appreciated.
As an aside, every one of my projects are showing 'Template Customization or Template Mismatch' errors. For the meantime, I am just trying to resolve the issues with this one main project. I may be able to forego migrating the Work Item Data for some of the other projects.
Thanks in advance!!!
In the move from 2008 to 2010 Microsoft changed a number of the built in fields. These fields do not appear in the template, but if you export the work item type from the instance of the process template you will see them.
Effectively an upgraded server has built in fields that never changed and the OpsHub tool craps out on fields that it should be ignoring.
The only solution, bar OpsHub fixing it, would be to try and change the field names to match what is expected.
You can use the witadmin.exe to make the change.

Inconsistent ADO.NET model after editing database

I am working on a ASP.NET MVC 4 app. I am using Entity Framework Database First design principle.
Initially I created the database on MySQL and then in visual studio i setup a connection to MySQL and generated the ADO.NET data model.
But later I added some tables in MySQL and then from Visual Studio I did this.
I can see the new entity types in the model diagram but no classes corresponding to the new entities have been generated in the Model.tt file. I mean classes from older tables are present, classes from newer tables are missing.
Please help!
Is your edmx nested in a folder? If so this might be a bug that was fixed in VS 2012 Update1 where code would not be re-generated if edmx was in a folder. Install VS 2012 Update 1 and the issue should be gone. You can also right click the edmx file and click Run Custom Tool option to enforce code generation.

EF 5 Update-Database Assistance

I am using Code First Data Migrations and have my development database so built up that I really don't want to start fresh again. I had deleted an old table and cannot get EF to accept any model changes due to this lingering phantom table. How can I get a clean version of my database context based on the model that's currently in place? Any help would be greatly appreciated!
You could use a "Code Second" approach and reverse engineer the model from the database. A good way to do this is to use the Entity Framework Power Tools for VS to do the reverse engineering. Once it is installed just right-click the project and select Entity Framework > Reverse Engineer Code First and select the relevant database in the dialog. This will generate a model in the Model folder.
I usually do this in a dummy project and then copy the files across. The model generation is actually very clean and adds some nice features like initializing ICollections to a new List in the constructor which are sometimes left out of the original Code First model.
EDIT
Here is an article which outlines the process using EF with an Existing Database