Sunday 8 April 2012

Entity Framework with MySql Database in asp.net 4.0

Hi guys, i am back again but this time i have some useful things, forgive me for my last post.

we will learn a good feature of asp.net 4.0 Entity Framework, there are lots of blog for how to connect sql server databases with Entity Framework in asp.net but a very few for MySQL Database. So trust me you are going to thank me for this blog, so today we will create 

 Entity Framework with MySQL Database in visual Studio 2010

step to step guide to connect MySQL database in Entity Framework with snapshots

Pr-requisite

1) You have installed VISUAL STUDIO TEAM EXPLORER EVERYWHERE 2010
if not please install it from here it is free to download.
Note: any visual studio 2010 will do.

2) You have MySQL server installed on your machine.
    if not please install it from here.

Steps to connect MySQL Database with Entity Framework in visual studio 2010.

1) First of all you need to download MySQL Connector Net 6.3.5 if you have it go to step 2
    else download and install it from here, Do not worry it is free to download as well :)
this connector is use to connect MySQL database from asp.ney

2) After installation open visual studio 2010 and create a simple web site project from file > new >web site
 shown below.




3) A pop up will come select asp.net website and click OK you will have a web site ready

4) Right Click on your website and select add new item




5) A pop will come with lots of options if you have visual studio 2010 you will get inbuilt feature to add ADO.Net Entity Data Model, select that and click Add.



6) A warning dialog box will come , just say yes and app_code folder will be created in your solution




7) Now is the main stuff that we are waiting for , if you want to create a Entity framework for existing database that you have already created in MySQL database click Generate from Database option and click next.




8) Now a Pop up will open which will ask for connection details to your MySql database.
    if you are setting up first time a blank drop down will appear, so click on new connection button to provide details about your MySQL database.





9) A POP will open that will have data source as Microsoft Sql server, click on change button next to it,
to change it to MySQL as we want to connect MySQL Database remember he he





10) A pop up will open which will have MySql Database option on left panel select that and click OK



11) After clicking OK it will go to previous Pop up . now you need to provide details about your MySQL database such as
Server Name : machine on which DB is present
User Name : username for that DB
password : Password for DB
Database Name : Database you want to connect

If you are using a development machine and your MySQL database is installed in same machine as Visual studio then you can give server name as local host
fill the details and click test connection if it is succeeded then we are good to go.



12) Now select one of the two options provided in below snapshot and click next to proceed
select yes for now.



13) After clicking on next a pop will open that will show your database tables, views, stored procedures
for mapping of table to Entity model select on tables check-box, you can individually select the tables from your database or if you want all tables then select check-box tables.

Just look at your namespace name as well this will be used afterwards. and click on finish button


14) whoa, you have Entity Model ready for your MySQL database, you can see Model.edmx file inside your app_code folder.just double click on it and you can see your tables in model


15) now you can write all type of code to get this entities for example i have written a simple code to get all entries from my question table in a Generic List type questionList

 protected void Page_Load(object sender, EventArgs e)
    {
        MyDataBaseModel.MyDataBaseEntities  db = new MyDataBaseModel.MyDataBaseEntities();
        List<MyDataBaseModel.question> questionList = db.questions.ToList();
    }


16)This is it now time to play around with it.... will get back soon with some different stuff bye..

3 comments:

  1. Very Good stuff and presentation is awesome!!
    Keep it up!! :)

    ReplyDelete
  2. Fine article. You have shown the 'Update Model from Database'. What about 'Generate Database from Model'? Have you got this working?

    ReplyDelete
  3. Thanks Morten, have not checked yet but planned to do the same.
    Are you getting any error while creating Generate Database from Model

    ReplyDelete