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
Related
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 a MVC web application that I've done code first to build my database.
I also need to have a console app manage data based on timeframes so, it will also need to access this database which I understand I can use as a Database First model.
However, I also need to build another website as a management dashboard, which I understand will also work as Database First.
Can I do this without having EF in one of the two circumstances nuke the database if I need to make a change to the model?
The short answer: no. You cannot implement both code-first and data-first EF on the same dataset without encountering a bona fide logistical nightmare.
Converting from one to the other is not quite as difficult as you might think, however, if your application is not overly complex. Based on the tables you've already created, data-first EF should produce objects that are reasonably compatible with your existing code.
Your next steps should look like this:
Pick one approach for EF
If necessary, convert existing projects to that paradigm
Move EF code into a shared class library (as suggested by snow)
Implement new projects using that class library to ensure consistency and reduce redundancy
I have an existing ASP.Net Web Forms application which is using EF Code First with Existing Database i.e. I am using EF classes and DbContext to point to an existing database.
I want to give the project the functionality to create/edit tables/fields and just wondering if people can recommend the best way to do this. I can't use migrations because the project is used on several different servers/databases.
I thought about putting something in the Global.asax file in Application_Start using SqlCommand. Is this a good idea or can you suggest a better way to do this, preferably with EF?
I'm not sure what if any code would be helpful so please let me know if there is anything you would like me to add to the question.
EDIT:
Based on answers so far felt I should also note that I cannot directly access the servers the application is installed on because they belong to clients. Project is deployed locally, zipped and uploaded onto a site for their download.
When using EF Code First, you should use EF Code First Migrations, although you say it's impossible.
Your only reason not to use this is the multiple database servers. Do you use Distributed Transactions?
Otherwise, the only variable is the ConnectionString to the database server, and EF Migrations will do all the work for you to update your SQL schema.
It's probably a bad idea to do this as part of application startup - it'll require that the user that your application connects to the database with has escalated privileges in order to create/edit tables.
You can use migrations to initialise a database as part of a deployment process using the migrate.exe which is part of the EF NuGet package.
It's probably also useful to read a bit more around migrations - there is support for multiple contexts that can each be migrated separately...
Alternatively you could use a SQL script as part of your deployment process but then you'd need to manage the SQL by hand...
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.
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.