Understanding Entity Data Model and EDMX file

ENTITY DATA MODEL AND EDMX FILE

Entity Framework is an API within the .Net Framework that serves up an "Object-Relational Mapper". It's a subset of the entire ADO.NET application programming interface. What this API does is that it allows to access or update Data via C# Code. 

From the lesson on how to create Database with MsVs, there I created a database ruining on a local server, this database could be updated or added to by using C# code. Of course, this is where the Entity Framework comes up as a "middle man" by serving a mapper that takes the Data from the Database and maps to a c# classes and properties that we could easily work within code. The EDMX file contains files that are generated automatically once the Entity model is created and among these files there would be a file name " .cs" as a  class that has properties we could work with. 


Read also: Enumeration

Therefore by creating an Entity Data Model, we basically create a series of entities that represent the data in the actual database, separate from the database itself. This would definitely create an abstraction layer from the main data that deals with details of "CRUD" (creating, reading, updating and deleting) data in the database.

Step 1 :  Create an entity model on LocalDbSample

Now right-click on the LocalDbSample in the Solution Explorer and select Add >New Item from the template select Data > ADO.NET Entity Data Model:Now Click "Next"  to create a "connection string" to the database you want the Entity model structured on.  This connection string is stored in a file that contains connection details needed for an Application programming interface , just like the Entity Framework, to get access to the database. Once these steps have been carried out, then under the "Name:" write a company name e.g, "SkyletEntities"  and click on "Add" button. On the next screen, there would be a room for selecting what we want the database designer to be (an option to model) there select “EF Designer from database,” under which data connection should your application use to connect to database? There select "Skylet.mdf" which is the database that we have already created with the SQL Server Database.

At the next screen, checkmark the database table that should be included in your Entity model as your database object. Obviously, we only have one table, which is Customers, therefore select that.


Step 2: Generated Class from Entity Model as a counterparts for Table Data located within EDMX file

Right in the Solution Explorer within our created project a Customer.cs class is generated among the EDMX file, taking the various data points in  the Customer table and convert them into acceptable properties:

The API Entity Framework knows how to convert between SQL data types( uniqueidentifier, varchar, etc) and .Net data types( Guid, string, etc):


Step 3: Using Code to Access the Entity Model

In the Page_Load() method within the Default.aspx.cs: 

  1. We need to set up a connection to the database we have created via code. 
  2.  This is done in the same way as we would create a new instance of a class; let see in code:
   namespace LocalDbsample
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SkypletEntities db = new SkypletEntities(); // set up connection in code to the database with Entity framework
var customers = db.Customers;
string result = "";
foreach (var customer in customers)
{
result += string.Format("{0} | {1} | {2}", customer.Name, customer.CustomerId, customer.Address);
}
resultLabel.Text = result;
}
}
}
 Save and Run:
https://www.maxybyte.com/2017/11/understanding-entity-model-and-edmx.html
Data from Database


No comments:

Post a Comment

Note: only a member of this blog may post a comment.