Create Entity Model of Data in Azure Asp.Net - c#

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.

Related

MVC database table relationships explained

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

Implementing asp.net Identity into already existing database with entity framework

I am trying to update an older website. It already has an existing database with a user and a role table, with existing data in it.
I need to create a new web-api and a new web project and integrate ASP.NET Identity into the already existing database.
I have currently setup my solution to breakup the projects as follows:
Domain
Services
WEB_API
WEB_UI
My aim is to implement the Identity in the services layer so that both my WebAPI and MVC site can utilize the same identity mechanism.
I am fairly overwhelmed at the moment, I have read many tutorials and articles over the last day or two and have ended up with the options of either re-implementing the IUserStore and IRoleStore. And also with mapping the different entities in the OnModelCreatingMethod.
I cant seem to find a tutorial which is aimed at what I want to do. My database is to different to simply remap the names of columns, And I dont want to re-implement the entire Identity Stores as there are only a few conflicting fields. Most tuts I have found are related to using mysql instead of EF. I still want to make use Entity Framework.
Issues I have:
The current Users Table in the database used Int pk , not GUID
The Password Field uses a different Hashing algorithm. So i would need to override how Identity checks and store the password.
I do not have all the required Identity User fields in my database, however I am able to add new fields, I just cant change already existing fields.
I am making use of Database First as the DB already exists.
So basically my question is, In what direction do I need to go in order to overcome the above mentioned issues. Can I get away with changing the Database mapping? Or do I need to go as far as re implementing the User and Role Stores?
What I had initially planned was to re-implement the User and Role Stores using entity framework, And i could then make use if the DB first model classes and map the actual DB structure and fields to my ApplicationUser Fields. But this is where I thought I might be diving into cold waters, and i'm generally not a fan of reinventing the wheel if not necessary.
-The first thing you should do if you haven't already is to BACK-UP your current database!
You could use code first to update an existing database.
I found this walkthrough and it seemed close to the route you're on, and I'm hoping it will help solve your problem, begin at "Migrating to Visual Studio 2013".
http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity
This would create a few new tables, but not necessarily a new database.
Download Nuget packages:
Microsoft.AspNet.Identity.EntityFramework,
Microsoft.AspNet.Identity.Owin,
Microsoft.Owin.Host.SystemWeb
Enable-migrations and run a script such as this: https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/SQLMembership-Identity-OWIN/Migrations.sql
Or you could try using a reverse POCO generator found in visual studio extensions.
(My 1st attempt at writing an answer here. Tried to clarify.)

Does an asp.net common page to CRUD all the tables in DB exist?

I want a page for super admin to CRUD all the tables in DB. Admin can use one asp.net page to select specific Table on a dropdownlist and can perform CRUD operation on that page.
I think that's a common topic for db access, searched but not found such module. Can anyone give me advice?
ASP.NET Dynamics Data might be what you need.
Dynamic Data supports scaffolding, which is a way to automatically
generate Web pages for each table in the database. Scaffolding lets
you create a functional Web site for viewing and editing data based on
the schema of the data. You can easily customize scaffolding elements
or create new ones to override the default behavior.
It doesn't exist as a built in feature so you'll have to implement it by yourself from scratch.
Assuming you are using SQL Server, a good start point is by querying system tables suchas systables and syscolumns, to set up the screen and later set up the CRUD operations.

How to create a database with Visual Studio, also using UI?

I made the UI side of the project, but now I need to have a database. How can I create it with Visual Studio 2010? I don't have any idea about how to retrieve data with C#. I know SQL but can you give me some examples of that (C# with SQL)?
What Joe talks about and what the post by Scott Hanselman describes is called "Code First". With Entity Framework, there are two other (easy) ways to create a database with Visual Studio 2010 tooling.
One would be to use a Entity Data Model (.edmx) file to create your model (classic Entity Relationship model) and then right click on the model and choose "Generate Database from Model", connect to SQL server and you're done. This is called Model First.
The other technique, which is called "Database First", is when you have an existing Database (or you can create your database directly from SQL Server), and you create an Entity Data Model based on that (you actually get to choose between Database first and model first from the Add-> Entity Data Model dialog). You select the tables, views and stored procedures you want to add, hit finish and you're set.
in both cases, When you build your soultion, you get a data context class that you can use to access your data which is pretty straightforward as well (pretty much the same way as described in scott's blog post).
There are lots of ways to do this, something you might want to take a look at is EF code first where you can create models in code and then generate the database based off of those models.
http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx

Need direction in creating an application which generates a crud form

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.

Categories

Resources