adding-migration DataAnnotations doesn't work? - asp.net-mvc

I have made a small MVC project and was trying to implement validation inside it.
following this tutorial
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/adding-validation
I made changes in my student table class.
and then tried adding the migration as it says but it says
No migrations configuration type was found in the assembly 'Bittu2'.
(In Visual Studio you can use the Enable-Migrations command from
Package Manager Console to add a migrations configuration).
I tried Enabling-Migrations and running the code but it now says
Creating a DbModelBuilder or writing the EDMX from a DbContext created
using Database First or Model First is not supported. EDMX can only be
obtained from a Code First DbContext created without using an existing
DbCompiledModel.
I have used databasefirst approach
Please tell what to do?

Related

Error generating initial migration: The type or namespace name "Migrations" does not exist

I have 2 Visual Studio projects; a .net core web API 3.1 project used as startup for ef migrations and a class library containing the dbcontext. When I open a powershell window, navigate to the folder for my class library and execute dotnet ef migrations add Initial --startup-project [path to csproj-file for the web API project] the migrations folder with a snapshot file and the initial migration file is created.
But when I open the migration file in Visual Studio I get several compile errors, one of them complaining that The type or namespace 'Migrations' does not exist in the namespace 'Microsoft.EntityFrameworkCore'... I can also see the auto generated migration file contains a using statement pointing to Microsoft.EntityFrameworkCore.Migrations which is marked as unnecessary and the documentation at msdn clearly contains both the base Migration class and MigrationBuilder class referenced by the compiler.
I tried looking for both Microsoft.EntityFrameworkCore.Migrations and Microsoft.EntityFrameworkCore.All on Nuget.org but none of them exist. Am I missing something obvious here? Why do the autogenerated migration files fail to compile?
Apparently I didn't look carefully enough at the msdn documentation for the Migration class. It is located in the Microsoft.EntityFrameworkCore.Relational assembly and after installing that Nuget package the project now compiles without errors.

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.

Pre generated views using entity framework power tool are not working

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'.

What should Id method do in Asp.net EF Migration

I've generated a file using dnx ef migration add CreateUserTable however unlike all the examples I find online my file is extending Migration instead of DbMigration and it is complaining that I need to implement public override string Id
The auto implementation of Visual studios just has a get method with an exception being thrown.
Question
What am I suppose to do with this and can anyone point me to some documentation?
Upgrade to rc-update1. It appears to be buggy in the beta version.

Entity Framework MVC Controller

I am using Visual Studio 2012. I created a Class project and added EF data model to this project. I created the data model from an existing SQL Server Database. I created a MVC 4 project, added entity connection string to web config, and added data project reference.
All is good to this point.
Now I want to add a MVC controller using Entity Framework. I select a one of the model classes. I select the data context class. Click Add.
VS pops an error up, the last part of the error message is class might be in a compiled assembly {which is true}.
'AA' is not part of the specified 'aanamespace.aaEntity' class and the entity class could not be modified to a DbSet property to it. For example the 'aaEntity' might be in a compiled assembly
I was in the same situation and I created a class library and in it i have all my .edmx and when i have a reference to my mvc project i got the exact the same error message i have posted my question here (stackoverflow.com/questions/18552864/…)
How did I fix:
Close the Visual Studio and open it again...
I want to take the time to answer this question myself so that others with a similar problem can be helped in the future.
Here is what I did.
Created the .edmx file in my Models folder of my MVC project.
Built the MVC project
Now I can add a controller for any of the tables in the .edmx file.
Note:
I thought the best practice was to create a project within my solution that holds the .edmx file. I called this project myData.
I added a reference to this project in my MVC project and tried this scaffolding without luck.
I hope this helps others because the solution is so simple.
Gerry
The problem was caused by the VSCommands for Visual Studio 2012 (Early Access) extension. Once I uninstalled it the problem disappeared.
Run Visual studio (Run as administrator) works for me.
Without seeing the exact error, I can only suggest a problem I experienced with the EF controller scaffolding and how I got around it. If you have extended your EF context class with a partial class, you will have to remove the latter from your project and recompile before generating the controller, otherwise EF scaffolding refuses to work.
Although this is a very old question, but there maybe people like me still facing the same issue which none of the suggested solutions can help them.
The reason for this error message when creating a new controller/scaffolding is the version of your EntityFramework.
HOW TO FIX:
if your project is MVC4 then you should use entity version 5.
if you use Entity version 6 you will face this issue.
remove your .edmx file and add your EntityFramework version 5.
it will work as expected.

Resources