I have an entity framework model that I created in one app, and I'd like to import(?) it into a new asp.net app. How is this done?
If you use a, for exemple, a Class Libary project for to make your EF model you can to add this project to another with "Project Reference"
I like to keep my EF models in their own Class Library projects. Then you can just open the existing project in any new solution in which you want the model to be included.
Related
I have two project in my solution. MVC and WebAPI. I added the WebAPI project later. I added new controller and Models folder in WebAPI project. But I can't create a new instance of my model in the controller. I get "The type or namespace name 'Category' could not be found..". It also doesn't allow me to bind the name using.MyProjectAPI.Models.
What should I do?
Right-click on Reference and click Add Project Reference
In the asp.net core projects:
What is the best way to share a controller with MVC Views to another project?
Is it any way to build a controller with Views as a component in the assembly and share it to the different projects.
In other words how to not just simply copy code and how to reuse it to another project.
Yes its a new feature in ASP Core called Application Parts.
It allows you to store your ASP Core Controllers/Views etc in a separate assembly and then you can include/share it as Application Parts/Dlls which is new in ASP Core, the sample code from MSDN is linked above for ASP Core.
In the ConfigureServices method of your Startup class just add this:
// set this up
services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();
Where assembly is the name of your instance Assembly with your controllers & Services, then you can load it either getting it from any included type or like this:
var assembly = Assembly.Load("YourApp.NameSace.AssemblyName");
Another nice ref implementation and explanation
Visual Studio 2015 + all updates.
Asp .Net Web application (MVC).
I start off by adding a few class libraries and separating the Asp .Net WA into layers i.e. DataAccess, Business Logic and the web project itself.
Once separated I add relevant references and everything is working as I expect it to be (i.e. the application functions as it did before I separated it into layers).
In my BL (Controllers are found here). I don't have the option to Add a Controller, like you would when right clicking the Controllers folder in the default project, so add the below line
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
to the csproj file for my class library and the Add Controller option now appears. Create a controller but throws an error which was due to not having a web.config file - add this file and all works (although it would be nice to have this library working without a web.config file).
The problem I've hit is, when the Controller is created it also adds a View template within the class library but my Views folder is located in my web project.
Have I done this wrong? Is there a workaround so when a controller is created, it also creates the Views into the correct project? Or another approach for this?
This is just a guess, but it seems like you are try to use a UI-based architectural pattern to build your business layer.
Typically, your models, views, and controllers are all maintained in the main web-app project. Any supporting functions (like your BL and DL) are added via class libraries. The Visual Studio MVC templates are built around that concept, which is why you had to manually add support with the GUID - and why it automatically creates the view.
If I may ask, why are you trying to build controllers into your BL? If you are trying to decouple your UI from your server code, perhaps WebAPI would be a better option.
UPDATE - A few helpful links
ProDinner - ASP.NET MVC Sample App
N Layered App with Entity Framework, Autofac, ASP.NET MVC and Unit Testing
Architecture Guide: ASP.NET MVC Framework + N-tier + Entity Framework and Many More
Most of your issues boil down to using the scaffold. The scaffold is great when you're just starting out or for extremely simple projects, but it quickly falls down beyond that. Specifically, adding a controller via scaffold is designed for an MVC project, so it expects to find things you'd find in an MVC project. Additionally, it creates scaffolded views in an appropriate directory in Views because, again, that's what it's designed to do.
The simplest solution, then, is to just not use the scaffolds. A controller is just a class that inherits from Controller. Nothing special there. Then, you can create the views where you want to create them.
I am trying to learn MVC. I want to automatically generate the required view code as and when I add a controller. This is possible if I select the option “Controller with read/write actions and views, using Entity Framework” . However I am not using Entity Framework. How can I achieve the similar behavior without using Entity Framework? And why it is unable to automatically generate the view when I don’t use Entity Framework?
Also, is there any good MVC3 tutorial that does not use Entity Framework (with code download available) ?
Reference
How do I configure ASP.net MVC to Scaffold using ADO.net dataservice?
Levergaing T4Scaffolding for WCF Web API
ASP.NET MVC 3 and NHibernate Scaffolding
Scaffold your ASP.NET MVC 3 project with the MvcScaffolding package
Once again LINQ to SQL or Entity Framework for new MVC 3 project
MVC Scaffolding for WCF Services
Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables
You might find some of what you're looking for in Steve Sanderson's MvcScaffolding package
Nuget
Install-Package MvcScaffolding
After installing (it will probably install some EF requirements) you could scaffold basic CRUD views for your model as follows assuming a model type MySweetModel
Scaffold Views MySweetModel
Please note this command will not create the controller class, but should create the following views under /Views/MySweetModel
_CreateOrEdit.cshtml
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
It looks like you might be able to override the default T4 templates, but I've never used MvcScaffolding outside the scope of EF. It's also possible someone has already done this for your persistence layer e.g. NHibernate or whatever you're using. I'd search a bit before implementing your own templates.
like the MVC Framework provided by MS to create folder structure?
You can use ASP.NET MVC to do MVP. Basically it's called MVVM: Model-View View-Model. Just add a ViewModel folder, add ViewModel classes, and bind your views to the ViewModels.
I am looking the same techie...
I found this:
http://dnnmvptemplate.codeplex.com/