Entity Framework Overview
Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.
Entity Framework is a part of the Net.Framework. EF allows us to access relational databases using strongly typed C Sharp core, or Visual Basic code or really any .Net language. There are three ways to access databases. Shema first, Model first and Code first. While working with classes to talk the Entity Framework, you don't have to worry about SQL connections, SQL commands, SQL parameters… There’s a few different ways to get started with the Entity Framework. One approach is called the Shema First approach. In this approach, you open up a graphical designer in Visual Studio, you point it to an existing database, and it can import the database schema and generate all the classes you need to query and update that database. Another approach is a Model First approach. We use the same graphical designer in Visual studio to draw a conceptual model for applications, so what classes do I want. Then EF generate both my class definitions and database schema. Finally, there’s a Code First approach you can take with EF. In this approach, it is necessary write C Sharp classes and the EF can use those class definitions to create a database. It will do that either using conventions like naming conventions. Naming conventions may make some things work if we write them in the right way. "Entity Framework" knows how to work with various types of databases (SQL Server, SQL Compact, Oracle, DB2…). We can create an application without much worries about the database type.
Code First - creating a new database
The first step after creating a new project is to create a Model. So right click on Models/Add /Class and add class (example 1).
using System;
using System.Collections.Generic;
namespace WebSkProject.Models
{
public class Student
{
public int StudentID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime EnrollmentDate { get; set; }
}
}
The second step is to create "Context". It allows us to communicate with the database and store data in tables. It is important to add here to use "System.Data.Entity".
using System.Data.Entity;
using WebSkProject.Models;
namespace WebSkProject.Contexts
{
public class RestourantDb : DbContext
{
public RestourantDb() : base("name=CodeFirst")
{
}
public DbSet<Restoraunt> Restoraunts { get; set; }
public DbSet<RestaurantRewiew> Reviews { get; set; }
}
}
Created "RestaurantDb" was inherited from "DbContext". For each class in the model, it is necessary to write the property „DbSet< TEntity >“. When it was created Model and Contex you have to write "ConnectionString" in the file „WebConfig“.This is all the code that should be written for storing data in the database.
<connectionStrings>
<add name="CodeFirst" connectionString="Data Source=DESKTOP-LJ085FJ\SQLEXPRESS; database = Restaurant; Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings>
The connection string is part of a global code that serves to connect to the server and to the database. In "connectionString", "name" is the name of the connection, "Data Source" is the server to which we are joining (in this case SQL localhost), and "database" is the name of the database we are creating.
Model First - creating a new database
This approach allows creating a model based on which database will be generated. The tool to create this model in Visual Studio is on Project/AddNew Item/Data/ADO.NET Entity Data Model/Empty EF Designer model. Once the installation is completed, Visual Studio opens a graphical interface for creating a database. Right clicking on the interface opens the drop-down menu where we select Add New / Entity.
In the new menu, we create the table name and "primary key". By right-clicking on table and „Add New“ we add the other properties of this table. When all tables and their links are matched, it is necessary to generate a database from the schema that we have made.
We do this by clicking the right click on the interface and choosing „Generate Database from Model“. In the next menu, we can select an existing connection to the server or create a new one. Also, we can select an existing database or create a new one. The next step generates a database based on the SQL script that was created in the project. After starting this script, a database will be created.
This tool allows you to start with a clean design interface and make a model, or to start with a database that already exists and generate table copies in this interface. It also allows for updating the database when changing the model. The Entity Framework is designed for business application development and supports many types of databases. The EF advantage is that it enables easy and quick operations such as creating, reading, updating, and deleting data. The biggest advantage is productivity. From the disadvantages it would be stated that if the database schema changes, the model will not be automatically updated.
Related Posts
Everyone would agree that 2020 was an unusual and challenging year that no one wants to repeat.
Airbnb Management Software helps owners to be organized in bookings and automate their business. Check out TOP 5 Vacation Rental Software and choose the best!
Modern IT companies usually offer many software development services, covering many areas of the IT ecosystem. Namely, they typically suggest expertise in the following directions:
Staying personal and secure on the web has become necessary than ever before. With the increasing online threats and government rules on websites, access to associate unrestricted, safe and higher internet could be a VPN away.
Learning how to code and develop software requires attention to detail and the determination to spend hours every day to perfect the craft. Software development has become one of the most sought after jobs in recent years, and technology is not going out of style.
Software test management tool is a decision-making tool that helps assessing software before it goes out to the market. Learn its importance and how it helps developers from software creation to its final development.
Comments
comments powered by Disqus