Pre generated views using entity framework power tool are not working - entity-framework-6

I am using entity framework with DB first approach. I have migrated from EF5 to EF6 successfully. In EF5, I was using pre generated views to improve startup performance. In EF6, it is not working. I have generated views using power tool. Power tool generated it successfully and I can build the application without any error.
But When I run it, getting following error:
The current model no longer matches the model used to pre-generate the mapping views, as indicated by the ViewsForBaseEntitySetsfc4437b421d2fd7f4d645bf31e3cb5b1b8374d9c77a07ef5f36c1cac0bfea31a.MappingHashValue property.
Pre-generated mapping views must be either regenerated using the current model or removed if mapping views generated at runtime should be used instead
I am not able to understand why there is difference between the hash value generated via power tool and generated by EF at run time. There is no change in the edmx file.
There are more than 290 entities in edmx.
Version of entity framework in 6.1.3.
Power tool is VSPowerTools-Beta4.
I am using visual studio 2013.
Code generation startegy of edmx is 'Legacy ObjectContext'.

Related

Porting Entity Framework 6 to Entity Framework Core

I am porting an application from ASP.NET MVC 5 to ASP.NET Core MVC and I reach out in the area pointing the Entity Framework related modules.
I have the generated classes and I also have a .edmx and .tt files, I added the files to a newly created class library (.NET Core). It seems like it needs to be regenerated (or I'm not sure), the .edmx cannot be opened (and there are no errors).
I have tried to manually update the derived models to use an updated libraries to use
using Microsoft.EntityFrameworkCore;
but I feel that these models needs to be regenerated instead of doing them manually.
One thing I observe is it also finds for ConfigurationManager instead of the appsettings.json for .NET Core.
What is the proper way of doing this?
Summary
I have .edmx and .tt files, and I have the generated files as well. How can I use it for ASP. NET Core 6.0 MVC, does it need to be regenerated?
What is the proper way of doing this?

EntityFramework (database first) using Azure managed identity for SQL DB

I'm in the process of converting the existing .Net framework project that uses EF 6 with Database First approach (EDMX) file to .Net Standard 2.1 which connects to SQLDB hosted on Azure.
Existing EDMX file has a constructor (auto generated) that accepts ConnectionString as given below -
public TestDatabaseConnection()
: base(ConfigurationManager.ConnectionStrings["TestDatabaseConnection"].ConnectionString)
{
}
This is breaking for two reasons -
ConfigurationManager.ConnectionStrings works only in .Net framework projects and throw runtime errors in the .Net Core project (for eg- ConfigurationManager.ConnectionStrings returns null).
I need to make changes to the above code to use the AppAuthentication nuget as mentioned in the MSDN docs here
So, what are the options available in order to make this work so that I could use the same nuget package for connecting to the database via EF 6 (database first) for both .net framework and .net core projects?
Is the only available is to convert to Code first and make the necessary changes as mentioned in the above MSDN link?

Entity Framework 6 not updating from database correctly in VS 2019

I'm working on a .NET MVC web app using Entity Framework 6 to manage a model from a SQL Server database. I've been using VS 2017 to develop everything so far, making changes to the database, and updating the model after those changes are made. When I switched to VS 2019 and attempted this, only the model and diagram of the model were updated. The classes generated by EF were not updated with the changes from the database. Does VS 2019 handle EF models differently than 2017?
According to this thread on VS Developer Community, it will be fixed in VB2019.1, in the meantime there's a workaround:
Save the .edmx file
Expand the .edmx file in Solution Explorer
For each .tt files inside it, right-click and choose Run Custom Tool.
For what it's worth, I'm experiencing exactly the same issue. The EDMX file gets updated as expected, but additional fields aren't being added to the generated class...
Reopening the project in VS 2017 works, suggesting that something has changed within VS itself.

No option to add EDMX file to project in MVC solution

I've created an MVC application. I would like to now add a new class-library project to the solution, and then add an EDMX file to that project.
This used to be easy. But now I have to choose between .NET Core and .NET Standard. But it appears it doesn't matter which I choose because if I create a class library with either of these frameworks, Visual Studio provides no option for adding an EDMX file to them! And I don't see an option to add a standard .NET Frameworks class library.
So what is the secret incantation to add a new project to my MVC solution, and then add an EDMX file to that project?
Note: I do have the option of adding an EDMX file directly to the MVC project. But I would prefer to place it in a separate project, which I've done before.
EDMX file is deprecated with EF Core and I believe it has been removed from .net core projects as well.
If you need to do that, you may need to add it to a normal .net 4.5 project and copy the files but this will make no sense using it with .net core or .net standard.
You should be using Code First anyway as it is cleaner and easier to use.
If you already have the database, you can refer to this link, it can help you to scaffold the classes from the database
https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
#Jonathan: The EDMX file is old way of doing things. EDMX is nothing but a T4 template which facilitates in creation of edmx.
With .Net Core the best way is to:
1. Create a .net core library project. Add entityframeworkcore nuget package from here
2. The use this link, to generate your model's from DB
The concept of EDMX is not supported anymore.
Hope this helps.
Based on your description you're using the old ASP.NET MVC framework, and not the new Core one, and in this case you're using the Full .NET framework. So, when you create the new Class Library project, you should do it with the Full .NET Framework, because .NET Core and .NET Standard does not support, EDMX. If you chose this one, you can add your ADO.NET Entity Data model to it:
At least for EF v6, if your using EF Migrations, each ('state'/'target') snapshot of the database (stored within the migrations-history db table & migration file) is an EDMX file just waiting to be decoded, saved to a file, and then loaded in VS (Visual Studio). ;)
It's a little in-depth (or at least challenging), but 'System.Data.Entity.Migrations.Edm.ModelCompressor' may present the clue.
Basically, you can extract the EDMX from one/the relevant migration. But I do agree that the Code-First approach should be used for the implementation, the EDMX is only a development-aid these days.

How to persist poco classes generated using EF 4.x POCO Entity Generator for C#

I am using EF 4.x POCO Entity Generator for C# for generating classes from database. I have put some data annotation on some classes and its working fine.
But when i made any change in the database and update the entity model, it removes all the changes in the poco classes (data annotations) because "EF 4.x POCO Entity Generator for C#" is persistence ignorant.
But i want to keep my changes. What should i do?
Very common problem. What you want is Buddy Classes.
http://hartzer.wordpress.com/2010/01/26/mvc-buddy-class/
Since POCO's are partial classes, you can create "buddy classes" (that persist outside of the EDMX ad regenerated code) for each POCO with Meta Data Annotations.
Your solution is to generate your code from the database using the entity framework power tools.
Additional db changes should be done to your code first and the use the database migrations feature of entity framework to generate your SQL script to apply to the database using the update-database command in the 'Package Manager Console' of Visual Studio
I go over a similar procedure here at the end on my DEV215 Entity Framewok for Real Web Applications Channel 9 video
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/DEV215

Resources