Using Entity Framework to Work With Database

Introduction

Today we'll talk about how to generate a database using the Entity Framework. The Entity Framework is to be referenced by the NuGet Package Manager in the application.

In that context, I am using the Entity Framework latest version to create the database while creating the application. I will explore many Entity Framework related articles. In here I am focusing on the use of the Entity Framework to create the database with a different schema. We'll proceed here with the following sections:

  • Creating Database with Model Creation
  • Working with Database Context
  • Working with Data Annotation Attributes
  • Indexes

Creating Database with Model Creation

Suppose we have a model with which we need to create a database schema using the Entity Framework. At first, the design is modeled with the following code:

public class University
{
    public int UniversityID { get; private set; }
    public string Name { get; set; }
    public ICollection<College> Colleges { get; set; }
}
public class College
{
    public int CollegeID { get; private set; }
    public University University { get; set; }
    public string RegID { get; set; }
}

In the code above, we've defined two classes or say entities for the database. There are various properties defined in each class or say we've defined the columns for the entities. Now for the context class follow the next procedure.

Working with Database Context

Now in this section we'll define the DbContext class of the Entity Namespace for defining the context for the class. Now define the context class with the following code in the model:

public class StudyDbContext : DbContext
{
    public DbSet<University> Universities { get; set; }
    public DbSet<College> Colleges { get; set; }
}

In the code above, we've defined the two DbSet properties for both classes named University and College.

Working with Data Annotation Attributes

In this section we'll define the Data Annotation attributes for the database schema. We use the Data Annotation attributes in the schema to get better string lengths and better names for the relation.

We've to modify the classes as shown below:

At first define the following assembly:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

The code is given below:

public class University
    public int UniversityID { get; private set; }
    [Required]
    [StringLength(25)]
    public string Name { get; set; }
    public ICollection<College> Colleges { get; set; }
}
public class College
{
    public int CollegeID { get; private set; }
    [ForeignKey("College_University")]
    public University University { get; set; }
    [Required]
    [StringLength(10)]
    public string RegID { get; set; }
}

In the code above, we've defined the Data Annotation for the Name in the University class such as this is the Required field and the length we can enter is 25 and in the College class the University is a reference of the University class and Registration ID is the Required filed and length we can enter is 10.

Indexes

Generally there are various type of colleges with their universities. We define the index on the CollegeID column.

Summary

This article described the database creation with the Entity Framework. You can also learn to apply the Data Annotation attributes in the database schema creation. Thanks for reading.


Similar Articles