Is it possible to use the MVC5 Scaffolding to create a new Controller with View, using EntityFramework if the Models and DbContext classes are not in the same namespace.
I have defined the models and dbcontext in 2 separate libraries (Project.Models and Project.DataAccess) and when entering the Add Controller menu the dropdowns for model and datacontext don't contain the classes I'm trying to use.
I have of course referenced them in the project.
Just make sure to compile your solution and then go to your MVC project and add reference to your model and dbcontext projects and that should enable you to see the model and dbcontext classes while creating controllers or views via scaffolding.
Sometimes MVC project some how catches the first reference. Even you build/rebuild library model, it doesn't update the MVC project. I got same problem and tried these actions:
Unloaded library project and reloaded it into solution
Deleted library model reference from MVC project and added again
Created DbContext class in Library model project.
Then it worked.
Related
I am trying to build a simplified module system for an application. At the moment I am trying out how to migrate data models by scanning assemblies in my modules. My project structure is as below
ABC.Core - All interfaces and common models & builders (Such as ASP.Net Identity related models). The DbContext is also in this project
ABC.Host - The host a web project which references core & plugins will be copied to /plugin directory.
ABC.ModuleA/B/C - each module will have it's own Models, ModelBuilders, Controllers etc..
In the CORE project DBContext I scan for models and do the migration which works fine. It creates the common models that is needed by all modules (like ASP.Net Identity derived models & few others)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyAllConfigurationsFromCurrentAssembly(GetType().Assembly, currentTenant);
}
Next update I want to do is to scan plugins in /plugin directory in HOST project and call for modelbuilder's Configure methods (Each plugin has IEntityTypeConfiguration model builders).
So basically I changed the onModelCreating method in CORE DBContext to get the dlls, do some reflection and call IEntityTypeConfiguration.Configure methods. It throws below exception.
The shared-type entity type 'ABC.Core.Entities.Authentication.ApplicationRole'cannot be added because the model already contains an entity type with the same name, but with a different CLR type 'ApplicationRole'. Ensure all entity type names are unique.
I made sure I don't have another ApplicationRole mistakenly placed even in another namespace. Only thing I can suspect is that my Host & Module projects all has a reference to ABC.Core project. Can this be a reason?
What would be a good way to do this kind of migrations for a plugin based project? Requirement is when you execute add-migration / update-database, it should scan assemblies and create the migration script (Even though models are separated, there will be one migration script). I don't want to have separate DBContexts for each plugin. I think through reflection we should be able to do the migration and also attach the models in plugins at runtime so the entire application can run through single Context.
I have a .NET Core 2.1 project where I have an MVC project that contains all of the controllers and whatnot. I then have a separate project for custom tag helpers. I need to access the assembly for the MVC project in my tag helper to get information about controllers using reflection; however, the problem is that I am not able to add a reference to the MVC project in my tag helper project because that would create a circular dependency. My first thought was to try and use the ViewContext class to get at the assembly info of the MVC project, but I have not had any luck. Is there anyway to pass assembly info from the MVC project to the tag helper, or do I need to move my tag helpers into the MVC project?
Hopefully, this question makes sense. Any help would be appreciated.
I was actually able to get around this using the magic of dependency injection. I created a class that accepts an assembly as a constructor parameter. Then since the dependency injection starts in the MVC project startup class I instantiated a singleton using
Assembly.GetExecutingAssembly()
I was then able to inject that singleton class into my tag helper.
I am creating a .NET Core web app using framework 4.6.2. I built my first view, testing throughout, until I added my view model and the page now errors out whenever I navigate to it. The errors are shown below. The ViewModel has all of the referenced properties on it. I don't even have a project.json given it's a brand new project on VS17.
This will happen if the properties you're using on your view model are not marked public. In my case, I had them marked internal and so was getting this error.
I am trying to decouple the Identity model logic from an MVC project, as it appears in the standard template with Individual Identity.
I have created a class library and copied over the IdentityModels.cs file.
But as I am adding NuGet packages such as Microsoft.AspNet.Identity.EntityFramework to solve the missing references problems, the references list gets filled up with dlls that sound like web libraries, e.g. MS.Owin.Security.Cookies, Newtonsoft.Json, etc. Also, the IdentityModels.cs uses IdentityDbContext, which is contained in MS.AspNet.Identity.EntityFramework.
My question is: is it possible to completely remove the web-related dlls from the models project to make it "pure" models and business logic, (i.e. to have no references to cookies or Json or aspnet) or is the Identity Framework so tightly integrated with UI and browsers that it is best to leave it inside the MVC project?
i have two projects that share one database. The Entity Data Model of the database is in a separated class library (i'm using Entity Framework 5).
So, one of the projects is an ASP.NET MVC project and i want to include the Entities in my Model, so i could use them in a WebAPI controller.
I have added a reference to the .dll of the database access library and i have included the ConnectioString to the web.config, so the database is fully accessible from my MVC project. But, if try to create a new Controller like this:
Add -> Controller -> WebAPI 2 Controller with actions, using Entity Framework
Then, at the "Model Class" section, Visual Studio does not shows my Entities to include them as Model objects, is it possible to make Visual Studio search for my entities at the .dll to automatically generate the controller?
And one more question: How does Visual Studio decide which classes to show as Model Classes and which doesn't?
Did you compiled your solution before you tried to add the Models in the new Controller dialog?