Here we will learn how you can create a
entity data model from an existing database (Known as database first approach). Please refer this to know what is a database first approach.
Create
an Entity Data Model In a Separate project :
Below are the steps to create a data model using database first approach (EF 4.5 VS 2012):
Below are the steps to create a data model using database first approach (EF 4.5 VS 2012):
Step 1:
Open Visual Studio 2012 > File > New Project > Windows
Template > Class Library > Give it a name
'EntityFrameworkModel' > OK
Delete the default Class1.cs that gets created in the project.
Step 2:Right click on the project in Solution Explorer > Add > New
Item Select the Data Template > ADO.NET Entity Data Model and
click Add.
In
the Entity Data Model Wizard, choose 'Generate From Database' and
click Next.
Create
a new Data Connection or choose from an existing one. If you want to
change the database connection then click on New Connection. A popup
will open.
Select
the Server name.You can either choose windows authentication or SQL
server authentication.In SQL server authentication you need to
provide the username and password. Then select the database name you
want to connect and click on test connection. If it will show a
successful message then click on OK button and proceed.
Step
3:
Now choose the Database Objects you want to include in your Entity
Data Model (EDM). Here I choose all the four tables 'Department'
,'Employee' ,'LogInDetails' and 'Salary'. As i dont have any stored
procedure and view in my database so am not going to select any SP
and view.If you want to change the name of the model you can change
it under model namespace textbox. Click Finish to complete the
wizard.
Then
you will able to see the .edmx file as below.
You
will find the connection string in the app.config file where you will
find find the schema files (.csdl, .mls, .ssdl) :
Build
the project in Release mode (Go to Build > Build
EntityFrameworkModel) and make sure that there are no errors. A model
defined by the Entity Data Model is now ready.
If you want to know how to use this entity data model in another .Net application to communicate with the database, then here you will find the link which will help you to achieve this.
Hope
this is helpful. :)
No comments:
Post a Comment