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.
Related
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?
I want to create a class library for an MVC 4 web application. Every search I've tried has returned plenty of references that merely mention creating one, or the importance of doing so, but not specifics of how.
My first assumption was a template would be under Web in the Visual Studio New Project dialog, but no. I was unsure if I was to use the Class Library template under Windows, but did.
I want to include things like some data access (e.g., DbContext), but while Intellisense sees the System.Data.Entity namespace, there are no classes available. I guess I need some additional references, but no idea which ones. Looking at the references in my main MVC project, at lot of them are pointing to the Packages folder. I'm unsure if I should be doing the same.
In short, I'm looking for instructions on how to create a class library for MVC in Visual Studio, including the necessary references for EF, Razor and whatever else.
you used the correct template - a simple class library is all you need.
then in the MVC web project just add a reference to the class library project
Use NuGet to add references to the pieces of functionality, like EF and System.Web.MVC, that you need in your class library or libraries.
A data access project to handle persistence and a class library to hold HTML Helpers that you might want to reuse both make some sense. Razor views if you're using the RazorEngine rendering stack can also be interesting to be able to test.
You are right to use the Class Library template in visual studio for your needs. You can add all of the references you need through NuGet (such as Razor, EF, and so on) and by Right clicking on references in the Solution Explorer and picking and choosing what you need.
Remember when using multiple projects that you add references between projects too! (for example your Web App project needs to know about your Data Repository Project)
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.
I've searched (google and SO) about this topic and couldn't find a thorough answer to my question(s).
I'm building an ASP.NET MVC 2 application that will be distributed to other people (with source code). These people will need to create modules/plugins that use the application's base.
The base is a simple ASP.NET MVC Application with Linq-To-Sql file, repositories, authorization/membership.
Is it possible to create a plugin that would work by simply adding a .DLL file in a folder?
Right now, you can create a "plugin" by opening the source project of the base application, creating a few controllers/views that do somethings, using the base application's authorization/membership and repositories. You would also be required to edit the Linq-to-Sql file and add in any tables that you need.
However, to "install" this plugin, I would need to copy the controllers/views for this plugin into my base application and edit the Linq-to-Sql class to include the tables necessary for this plugin, then build the solution. Is there a simpler method?
I read of .DLL plugins, but how would someone build a plugin like this starting from the base application.
If the 'plugin' creates tables with foreign keys of the "User" table in the main application, how does one separate those tables/relationships in a separate file and have the base application recognize those relationships?
As you can tell, I'm asking multiple questions that are kind of all over the place. This is a new topic/issue for me and I have no idea where to start. Theme mere concept of having my application interact with a separate .DLL file is foreign to me.
Any help/links would be greatly apprecaited.
Does this answer the same question: Plug-in architecture for ASP.NET MVC?
I think this could be applied to mvc too: http://msdn.microsoft.com/en-us/library/ms972962.aspx
I've built ASP.NET webform projects in the past, and when generating Subsonic classes, the teams I have been on have put our Business Layer/DAL objects into a Project.Framework project.
Would that still be a recommended structure, or should the Subsonic classes go directly into the /Model folder within the MVC web project?
It shouldn't go in the Model folder (I think the Model folder should just be used for a Class that is made only for the view and wont be used in the rest of the app).
It should go into a separate assembly Maybe Project.Core or Project.Data
I would not put the Subsonic classes directly in the MVC project. Since you didn't with ASP.NET, there is no reason to change now.
I wouldn't even leave the Controllers in the MVC web project.
You can put them in Model, and move them to their own project later, but its not really any more work to just put them in their own project now, so I would just do that.