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 { getprivate set; }

    public string Name { getset; }

    public ICollection<College> Colleges { getset; }

}

 

public class College

{

    public int CollegeID { getprivate set; }

    public University University { getset; }

    public string RegID { getset; }

}

 

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 { getset; }

    public DbSet<College> Colleges { getset; }

}

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 { getprivate set; }

    [Required]

    [StringLength(25)]

    public string Name { getset; }

    public ICollection<College> Colleges { getset; }

}

 

public class College

{

    public int CollegeID { getprivate set; }

    [ForeignKey("College_University")]

    public University University { getset; }

    [Required]

    [StringLength(10)]

    public string RegID { getset; }

}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all