EFCore ASP.NET Application
Register the Entity Framework Core Provider
Complete the following steps to register the Entity Framework Core provider:
- Add a reference to System.Data.CData.Email.dll, located in the lib -> netstandard2.0 subfolder in the installation directory.
- Add a reference to CData.EntityFrameworkCore.Email.dll:
- For EF Core 3.0, this is located in the lib -> netstandard2.1 -> EFCORE30 subfolder in the installation directory.
- For EF Core 3.1, this is located in the lib -> netstandard2.0 -> EFCORE31 subfolder in the installation directory.
- For EF Core 5.0, this is located in the lib -> netstandard2.1 -> EFCORE50 subfolder in the installation directory.
- Add the included licensing file (.lic) to your project. You can accomplish this by right-clicking the project in the Solution Explorer, then navigating to Add -> Existing Item -> System.Data.CData.Email.lic (located in the lib -> netstandard2.0 subfolder in your installation directory). Finally, right-click the licensing file, and set Copy to Output Directory to Copy if newer.
- Build the project to complete the setup for using EF Core.
Creating the Data Model
There are two approaches that can be taken in creating the context and entity classes for your application. With the Code-First Approach approach, you can fine-tune your model by writing the classes manually. Alternatively, you can make use of Reverse Engineering (Scaffolding) to generate these classes automatically from your IMAP schema.
Registering the Context with Dependency Injection
In order for the MVC controller to make use of the EmailContext, you'll need to register it with dependency injection. Add the following to the beginning of your Startup.cs:
using MySolutionName.Models; using Microsoft.EntityFrameworkCore;
Next, find the ConfigureServices method in Startup.cs and add the following at the end:
var connection = @"Port=993;Server=imap.gmail.com;Password=password;User=user;Protocol=IMAP;SMTP Port=587;SMTP Server=smtp.gmail.com;"; services.AddDbContext<EmailContext>(options => options.UseEmail(connection));
Creating a Controller and Views
To create a controller and views for your web app, follow the procedure below:
- Right-click the Controllers folder in the Solution Explorer and navigate to Add -> Controller...
- Choose MVC Controller with views, using Entity Framework, and click Add.
- Set the Model Class to the class corresponding to your table/view and set the Data context class to EmailContext.
- Click Add. Note the name of the controller.
Running Your Application
Now that the controllers and views have been setup, you can launch your app using Debug -> Start Without Debugging. The app will then launch in your browser. You can find your data by navigating to <Base URL of App>/<Name of controller minus the 'Controller.cs' at the end>.