Published: Friday 8 September 2023
Entity Framework is a popular ORM which is supported with SQL Server.
It allows us to call database queries using SELECT, INSERT, UPDATE and DELETE from an .NET Core application.
Take this model and service:
public class Contact {
public int Id { get; set;}
public string Name { get; set;}
public string Email { get; set; }
}
public interface IContactService {
Task CreateAsync(Contact contact);
}
public class ContactService : IContactService {
private readonly MyDbContext _myDbContext;
public ContactService(MyDbContext myDbContext) {
_myDbContext = myDbContext;
}
public async Task CreateAsync(Contact contact) {
}
}
We have a Contact
class that contains properties including the name and email.
In-addition, we have a ContactService
class that injects an DbContext
instance called MyDbContext
.
Finally, we have a CreateAsync
method which passes in a Contact
instance as a parameter.
The method is currently empty. Your job is to add the Contact
instance as an asynchronous call to the MyDbContext
. In-addition, you need to save the changes to the database.