I was wondering if anyone knew of any way i can implement an application which will do the following.
Allow a user to specifiy a connection string to a sql db
Allow a user to specify a table in the db
Allow a user to specify columns from the specified table
Generate Views, a Controller with Crud methods, & Data access code on the fly for the specified table columns in a subdirectory on the current web app.
I'm aware that there are apps that currently do this (such as sharepoints list creation stuff), but i'd like to see how this was accomplished and recreate it for my own learning purposes.
Thanks alot for any help
Take a look at Microsoft's take on scaffolding, also, some time ago I was developing a taxonomy app and found this meta data model in codeproject
Edit: another cool SO link
Have you check out SharpArchitecture?
Anyway I fiddle with MVC 2 based AutoCrud when I'm not saving the world from aliens so I can give some pointers and point to things to check out:
Become familiar with how MVC 2 can auto scaffold up your edit screens
Understand that you'll have to pass "meta" information about your models somehow. In MVC 2, this is called ModelMetadata.
Tackle how to display related or associated models in aggregate root or parent screens
Learn how to generate code, and inspect ddl schema or meta information with T4 templates.
Thats all I can think off for now. This is not an easy task and a comprehensive answer is probably enough to fill a book.
Related
I was hoping someone would help explain some of MVC to me.
Say for example you have,
var orderDetails = new List<App_Orders>();
App_Orders is a table in the database that the application is using. So I say orderDetails is a List of type App_Orders?
In this example, say the user clicks on a link to open an existing Customer, on screen is listed all the Customers Orders. The code gets the Orders that are on screen...
List<App_Orders> ordersInSession;
.....
ordersInSession = (List<App_Orders>)Session["Orders" + id];
If my understanding is right of MVC and the way the tables in the database are joined, if a table has a relationship with App_Orders, it will be available when accessing ordersInSession. For example, App_Orders has a relationship with App_OrderItems and Ref_OrderType. An Order can have one or more Items and the Orders are of a particular Type. So when you type, ordersInSession. , intellisense will pop up with the list of properties of App_Orders, for example, OrderID, OrderDate, but will also have App_OrderItems and Ref_OrderType.
Am I understanding that right? If the table has relationships, then those tables are also available? If I am, would you call those attributes?
Sorry I'm not even sure that made sense, but if anyone has any idea what I mean if they know of a tutorial that I can read to help me understand this part of MVC better.
Thanks in advance
ASP.NET MVC is a framework for building web applications, it has "nothing to do" with databases and is not providing facilities to manipulate them.
The idea is that the user asks a Controller to show data (Model) on a "page" (View). Nothing to do with databases.
What you want probably is to add some code in your MVC Controllers so they build a Model from objects in a database. For this your can use an ORM framework like Microsoft's Entity Framework, or lightweight/easy things like Dapper or PetaPoco.
Your MVC controllers can then load data from the database, construct a list of App_Orders and give that as a Model to the View to display.
Avoid storing things in the Session as much as you can, it's not meant to hold volumes of data and it's asking for trouble e.g. if the browser opens multiple tabs in the browser.
Are you using any ORM framework?
For Entity Framework you can read about relationship here
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
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.
I have created an ASP.NET Azure WebForms application. It has its default database and I can access it & modify AspNetUsers table using IdentityModel.cs (EntityFramework) as shown in many sites.
Now I have created other tables in the database namely "Projects", "customers", etc. and also have designed aspx forms based on those fields. Now I am not able to get, how to associate these forms wit the tables in the database. I wouldn't prefer Scafolding as many sites show, instead I would prefer something like IdentityModel does. I can have control to retrieve data, display, show, edit, add functions based on my button clicks or so.
I read many tutorials & blogs but couldn't get how to achieve this as I want. Tutorials shows using Scaffolding & MVC. I also looked to add "Entity Data Model" in projects, but none create .cs file; so again lost their. Can anyone please help me know how can I achieve what I am trying to.
Please any help is highly appreciated.
What I think you are looking for is the ability to reverse engineer a model from existing tables. This is possible, although can be a little painful if your existing database has a complex schema. If you are using Studio 2013 you can add a New Item of type ADO.NET Entity Data Model and on the screen that appears choose Code First from Database. You need to then point the wizard your db connection string. This will create the context classes, entity classes and configuration that you need.
More details are in this document from Microsoft.
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".