How to add a model in asp.net mvc project - c#

I recently made my first ASP.NET website with MVC. I selected the option that pre-loads a basic project with login functionality and a few pages. I spent a couple hours learning how it all works and I'm pretty comfortable with most of the features. Now I want to add a class that interacts with the database and I've run into a bit of an issue.
When I search for a solution every response says to use DbContext. I don't think there's necessarily anything wrong with this, but when I search for DbContext and some other commands that show up frequently in these responses, there are no instances of them in the project. I would really like to use the same method of creating models that was done for the account classes, but when I look at the code I'm not entirely sure what I'm looking at as it links to a bunch of different files.
Can I get some tips on how to create classes the way that ASP.NET creates default account models?
For reference: I'm using Web Essentials, Productivity Power Tools and VS 2013.5

There's tons of tutorials online that cover ASP.NET MVC Code First Entity Framework. I start with something like this
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
And see where you get to. This is the basic approach:
Create the model classes
Create the Context
Enable Migrations (in case you need to change the model)
The SQL Expres database will be built automatically based on your EF classes.
Good luck. It's not a difficult as it first appears once you've done your first one.

Related

Best course of action to 50+ databases with over 100+ tables/views

I've been tasked with making a web application that will display certain data from multiple databases/tables/views. I currently have been learning ASP MVC 5 to display data and I've been able to successfully connect to a database and display the information needed using Entity Framework 6. However, my issue is that this will become very tedious to continue doing this for multiple databases/tables/views even using scaffolding.
My current thought process is to go about making dynamic views/controllers or even have a way of programmatically creating views/controllers. I don't know if there is a way for me to create entity framework models at run time? I also don't know if there is better solutions out there to do something like this.
Follow up question, is it better to just use ADO .NET to access all this information? Or is there a way for me to just create a connection string and a new dbcontext/entity and then just connect to it that way without needing to generate the whole model?
Any help is appreciated!
If you choose to use Webforms and ASP.NET Dynamic Data, it will scaffold an entire database for you instead of scaffolding each controller one by one.
You can see more about it here: https://msdn.microsoft.com/en-us/library/cc488469.aspx

Are the M and the V from MVC even needed in ASP.NET Web API?

I have a Web API application that uses one controller which returns data read from a file.
So all I'm using is the C (Controller) part of MVC. I have no Views, and I'm not using Models, either - I'm storing and retrieving the data to/from the App_Data folder.
I know this is a rather simple solution, but it seems that all I need is C, not MVC. Am I missing something?
Model-View-Controller is a design pattern, to help structure applications that have a GUI. ASP.NET MVC is a framework that helps you build a web application according to this design pattern.
Web API has nothing to do with the MVC design pattern. It was developed separately, temporarily distributed under the same umbrella name and now an own framework altogether: http://www.asp.net/web-api
The fact that some project templates still create "Model" and "Views" directories in your project, means that you're using an outdated project template or one that's assuming you want MVC and Web API in one project.
CodeCaster's answer already explained very well the "theory" behind your question. Although, I'll contribute with an answer because I stumbled into this months ago and it took some time to figure out why a recently created Web API project also had the Views and all the other items that a ASP.NET MVC application has.
The thing is in Visual Studio 2013, there's the Web Application template. When you select this option, a window is opened. In this window, there is an option for creating a Web Api. The problem relies here. When you select the Web Api in the window (Which of course it's what you want), for some reason it also mark the option for MVC in "Add folders and core references for" and worse, the checkboxe's are disabled so you can't "uncheck" it (so weird): This is what it looks like:
If you hit Ok at this time, the API is created with all the MVC references. Probably not what you want.
So, to create a "pure" Web API, that only has references for Web API related files and structure, this is what you need to do:
Select the Empty template and then mark the option for Web API. :
Consider that your controller has actions that will be invoked from a front-end application. The data being sent as the result of that call can be thought of as the model. The front-end app may then use that model to display a view. But you might also.do nothing of the sort. It is up to you as a developer to properly use the MVC pattern if that is the requirement.
Almost all software patterns start to make most sense as a project increases in size/complexity, and where the scenario in question mirrors the assumptions underlying the pattern. There are a number of trivial scenarios where the Model and View components are either implicit, provided by the framework itself or simply unnecessary. What the MS ASP.NET MVC does in essence is provide a framework that implements the basic MVC pattern within the MS web ecosystem. Whether you need to use the individual features (like EF modelling, Razor views) is up to you.
From your question, your requirements essentially are to allow access to a stored procedure and perform basic data transformation (via the POST) and then to return that data as JSON (via the GET). What is the necessity of the separate GET/POST requests? From your question, the GET relies on a preceding POST request. I would suggest that creating the intermediate JSON file may be unnecessary, rather just look up the data and pipe it back.
Should we not consider the data returned by the controller action as a view, A view of your data rather than a HTML view, the design pattern does not specify the type of the view. In my opinion its still MVC, the model still exists and you normally would create model of your data that you want to return, the view part is just an other model or view of your core model classes.

ASP.NET MVC3 and MongoDB

I would like to build a new web application using ASP.NET MVC3 and MongoDB. I've seen many examples online and even built some working code myself, but I am wondering about how I should set up my application. In the MVC examples which use Entity Framework, they place everything in a Models folder. I think I will do the same but where should I put my queries etc. Should I abstract them to a better location. I'm somewhat new to making C# applications and the .NET world, so some of the "ways" are not clear to me yet. Also, does creating the database object (where I tell it mongo's server address) each time I need it have performance impacts? Can I just connect once and then talk through that object? Does it really reconnect every time I perform that action?
Thanks!
Normally, in your Model, you have an object model representing your domain.
With MongoDB, this does not change. Your objects in your model will still have properties and behaviors.
What will change, is that instead of storing each object in a table in a relationnal model, you will be storing a graph of objects. Let's say you have an invoice. You will store the Invoice, with all the lines of the invoice as a single record. That's about it, not really more complicated than that.
First of all, don't use your domain objects (these that you supposed to save to RDBMS using Entity Framework or to MongoDB) directly in ASP.NET MVC views! Use viewmodels instead. Then you will have Models folder in ASP.NET MVC project and separate project for your domain.
I didn't work with MongoDB before, but I suppose the best way to have database object per http request. Here is discussion on stackoverflow and here is video from 10gen about their C# driver.

The difference between building a Rails application and an ASP.NET MVC Application?

First of all some background: 4 years of C# experience and a year of Rails experience. I would appreciate any input from someone who has worked in both and knows a lot about developing ASP.NET MVC applications.
I've been doing nothing but Rails projects for the last year. Now, I have a client wants their application converted from ASP.NET Web Forms to ASP.NET MVC. This is the first time I've done MVC in C# so I'm trying to see how different things are and if certain productive Rails tasks map over to ASP.NET MVC.
First of all, is there such a thing as a Scaffold in ASP.NET MVC? I see something called an Area but I don't know if thats quite what I want. Also, how can I generate a scaffold (models, controllers and views), just a controller or just a model based on the same information I would give a Rails app?
For example I might do something like:
$>script/generate scaffold person first_name:string last_name:string
which produces a Person model, a migration script (what I run to build the database table), a People controller and views for each of the RESTful interfaces (index, new, create, edit, update, show, destroy). Can I do something like this in Visual Web Develop 2010 Express?
There is MVC Scaffolding with MVC3.
Here's a nice post on it.
Whereas Rails has tries (especially for beginners) to guide you into one way to write your app, MVC attempts to be all things to all people. So it's very flexible, but it's hard to specify the "one true way" to scaffold something.
So one way which works is:
Create your DB. Create an Entity Framework model in the usual way from the DB.
Compile.
Right-click Controllers, Add, Controller. Check the box for actions.
Right click one of the generated actions, choose Add View.
Check the box for "Create strongly typed view" and select scaffolding from the combo box.
But there are many other ways!
There are 3rd party tools for migrations, but nothing built in. What is built into full VS (maybe not express) is database comparison and merge script generation, an arguably more powerful, but perhaps harder for new developers to understand, alternative.

Generate simple web site GUI using .net

I have a couple of super simple databases. Basically they all consist of single tables that saves tracking/logging data etc.
Now I want to list this data but I'd like to find a solution that's applicable to all the different tables in all the databses. SO basically I'm looking for some way/some pattern of pointing a solution to a database, generate code and GUI and the publish the site. The tables can have huge amount of rows so I need functionally like paging etc but otherwise a simple list is all I'm looking for as a first step. I've been looking at Dynamic Data from MS - could this work? Other options? I'd really like the web sites to me ASP.NET MVC ...
As a seconds step I'd also like to have the possibility to change/add and delete data from the rows via the GUI.
Consider using ASP.NET MVC 2. It has extensive scaffolding capabilities that can auto-generate the views for you, yet allows a lot of fine tuning
Brad Wilson explains scaffolding here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
For MVC 2, see
http://aspnet.codeplex.com/
and
http://www.asp.net/mvc/download/
Paging is not included, but you can get that from MVCContrib, for example
You could roll your own scaffolding and generate the actual pages using T4 (http://www.olegsych.com/2007/12/how-to-create-a-simple-t4-template/ and others), or you could look into monorail (http://www.castleproject.org/monorail/index.html).
If you don't mind mandatory JavaScript then I would suggest jqGrid. It should be very simple to enumerate tables, columns, and generate jqGrid's columns from it (colModel). You can see an example of how I do a similar thing here - it does almost exactly what you need but for classes, not database. So, you can't use the solution "as is" because it's for domain classes, but it is very close, you can take it as a base and rewrite JqGridExtensions.JqGridModel to process db table definition, not domain class definition.
If your site has to work without JavaScript, then either MVC v2 with scaffolding will work, or you can try S#arp Architecture which also includes its own scaffolding out of the box, that generates full CRUD model/controller/views/repositories/etc. The benefit here is that you will still work with classes, not datasets. However as far as I understand you'll have to define scaffolding manually for each entity - but since scaffolding is very flexible, I'm sure you can write Uber-Scaffolding class that will enumerate your database and spit out sub-scaffolding files or just call them right away. A quote:
you programmatically create an object
called EntityScaffoldingDetails which
holds details such as the entity name,
the namespace, along with property
details such as the property name,
type, domain signature inclusion,
attributes, default test values, and
SQL generation details.
But, I better like the jqGrid way, because it's very easy to generate jqGrid model definition from the database schema which usually contains all the metadata. Of course, one can integrate S#arp scaffolding and jqGrid so that the first produces the latter. The benefit of scaffolding is that you have full control over the code, you can take it and tweak it. Because, you can't expect it to be 100% automatic - generator can't decide HasMany or ManyToMany, control type, hidden fields, etc. Of course you can use SQL metadata (properties maybe) to give hints for your scaffolding generator.
Now, if you need a ready out-of-the-box solution, I don't think there's one except for maybe commercial tools. The above will only allow you to assemble your own one. As for commercial tool, for example, Telerik Grid said to be able to "automatically infer its columns based on the properties of the data item it is bound to".

Categories

Resources