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

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

Related

.Net MVC Database first model with stored procedures

I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.
My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.
Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it?
Thanks!
If it is code first then you can use the EF tools to scaffold your database for you from your existing database. If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.
If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object. These should really match precisely the data that is being returned back in both type and naming:
this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.

Create Entity Model of Data in Azure Asp.Net

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.

In C# is it possible to create a model of entities regardless of the database initially?

In C# is it possible to create a model of entities regardless of the database initially. Are there tools to create graphic entities in this way? What do you recommend?
Well, it's in your question as a tag - entity framework is a good tool for this. You can create classes, that you push later to the database.
Search for "code-first". Google will provide plenty of results I'm sure.
Edit based on the comment:
Well, in that case, try this: when you're creating edmx file, pick the second option - empty model.
From its description:
Creates an empty model as a starting point for visually designing a
conceptual model from the toolbox. Classes are generated from the
model when the project is compiled. You can specify a database
connection later to map the conceptual model to the storage model.
this may be the thing you want then.

Entity Framework Multiple Stored Procedures

I am quite new to the Entity Framework, and only have recently started looking into it. I have been using Linq to SQL for sometime now in a C# enviroement and found it really wonderful to use.
Currently I use sqlmetal to generate a DataContext File (Linq to SQL).
Now after some time I thought it would be nice to use to the Entity Framework, (Linq to Entities), I can see that in some respect there are syntatical similarities between the two, i.e. accessing and creating new instances providing the connection string.
However what Im interested in is when the mapping is generated, is there a way to automatically import all the stored procedures, similar to how sqlmetal does it. So that I dont have to import each one individually.
Thank you in advance.
In the Model Designer (inside Visual Studio, default view option for *.edmx files)
right click --> Update Model from Database
in the "Add" tab of the resulting dialog you can select any or all Stored Procedures.
Edit: Here's a screenshot of the dialog I'm talking about, found at a tutorial at robbagby.com

I've created a database table using Visual Studio for my C# program. Now what?

I'm very new to C#, so please forgive me if I've overlooked something here. I've created a database using Visual Studio (add new item > service-based database) called LoadForecast.mdf. I then created a table called ForecastsDB and added some fields.
My main question is this: I've created a console application with the intention of writing some data to the newly created database. I've added LoadForecast.mdf as a data source for my program, but is there anything else I should do? I saw an example where the next step was adding a "data diagram", but this was for a visual application, not a console application. Do I still need to diagram the database for my console app? I just want to be able to write new records out to my database table and wasn't sure if there were any other things I needed to do for the VS environment to be "aware" of my database. Thanks for any advise!
Well, it depends on what data access technology you want to use. You can use straight SqlClient, you can use untyped data sets, you can use strongly typed data sets, you can use LINQ to SQL, you can use Entity Framework.
If you're not sure, then probably the most fun would be LINQ to SQL, so I recommend you follow a tutorial first and then come back to your app. Eg. ScottGu's LINQ to SQL (Part 4 - Updating our Database), or MSDN's How to: Insert Rows Into the Database.
There are also video guides, like VS2008 Training Kit: Using LINQ with Relational Data or MSDN Webcast: Using LINQ with Relational Data (Level 100).

Categories

Resources