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.
Related
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.
When I try to run my application, I ge the the following error;
I know this issue has come up many times, and the usual answer is to right click on the appropriate project and make it the start up project.
Well I have done that and this does not fix it.
My application is an MVC application, so the start up project should be a class library.
It was working fine until I tried to upgrade it to MVC5.
I am working on VS 2012.
I am wondering if the problem was caused by unloading and reloading the csproj file which I edited as part of the upgrade?
I think it might have something to do if there are multiple projects in the solution. I was getting the same problem when I had a MVC project and the testing project in the same solution. What fixed it for me was setting the MVC project as the StartUp project. This is done by right clicking on the MVC project and selecting "Set as StartUp Project".
This works for VS2013 Professional
I had the same issue and corrected by altering the projecttype guid. Here is an example:
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
I had a solution that included both an ASP.NET MVC web project and a WebAPI project. The solution was created originally with VS2012, but I recently upgraded it to VS2013.
I just noticed when going to add a controller that I'm not getting the correct options in the right click menu. For instance if I go to the web project and right click the controllers folder, the option to add a controller isn't there. Instead I have add WebAPI controller. It's exactly the opposite for the WebAPI project.
Things seem to build and run ok so far, but it's going to be a pain manually adding things and I'm wondering if something else might break. Any idea why this is happening or how to fix it?
I would guess that project type is different in your csproj. Take a look at this question:
What is the significance of ProjectTypeGuids tag in the visual studio project file
Here is what I have on my machine for c# asp.net mvc project:
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Take a look at your guids and see what they mean.
You could see the meaning of the different GUID in the register :
•HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Projects for
ProjectTypeGuids
•HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Packages for
packages reference by some project
Using Visual Studio 2013, create a new ASP.NET Web Application project using the MVC template. Verify that the Add View dialog functions by right-clicking the "Views\Home" folder and choosing "Add... View".
Now add a new F# Library project to the solution. The Add View dialog should still function correctly.
Now add a project reference from the MVC project to the F# project by right-clicking the References folder, choosing Add Reference, Then browse to Solution...Projects on the left and checking the F# library project that you just added.
At this point, if you right-click the "Views\Home" folder and choose "Add... View" you will get an error in a pop-up alert box instead of the Add View dialog:
There was an error running the selected code generator: 'Object reference not set to an instance of an object.'
Workarounds include, but are hopefully not limited to:
Remove all your references to any F# projects every time you want to add a new view.
Unload any referenced F# projects, but leave the project references in place, every time you want to add a new view.
My questions are:
Are other people seeing this behavior, or is it just me?
Does anyone know of a better work-around than removing references or unloading F# projects?
Does anyone know if Microsoft is already working on fixing this? If not should this be reported to the F# team or the Visual Studio team?
Between this issue and the fact that it is still impossible to use NuGet to add an Entity Framework 6 reference to any F# project, I'm starting to get nervous about Microsoft's level of commitment to the F# language.
Not sure if this will help you but check the version of EntityFramework in your solution. I had two projects within my solution and for whatever reason, the web project had EF 6 and the class library project was EF 6.1. Once I uninstalled EF completely from the solution and reinstalled EF 6.1 on both project, the "Add new view" action started working.
I received the same error message when adding views or controllers in a C# & MVC multi-project solution, using EF 6.1 and VS2013 with Update 3. The problem was because the nuget "packages" folder was marked as read only, because it was checked into source control. I checked the entire folder out, making it editable, and it started working fine, allowing me to scaffold straight from VS.
I also received the error message in a web.api project because I forgot to add the Entity Framework connection string to the web.config (it was using Areas and EF was looking in the Area web.config, not the web.config in the root).
I am trying to use an Entity Framework Model (in separate project) into an asp.net 4 MVC project (VS2010, C#)
If I create the EF inside the MVC project I have no problems.
I think I am missing some step.
things done:
1. added reference to the EF class project
2. added connection string in MVC web.config
3. added reference to System.Data.Entity in both web.config and project references
Now i can use the model only if I copy the .edmx file to the Models folder, but in this way the EF project is not external anymore.
What am I missing?
Thank you in advance.
Have you set the entity model to create public classes? (Set in the models properties, and each of its entities.)
You seem to done all necessary steps already. Try to remove the reference and add it again and rebuild it.
I had same problem but after rebuild the solution (1 MVC project and 1 data access layer project), everything works fine.