- Home
- .NET coding challenges
- Add a new record using Entity Framework
Add a new record using Entity Framework
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.
Give us your anonymous feedback regarding this page, or any other areas of the website.
Latest challenges
Convert hours, minutes and seconds into a time formatted string
Pass in the hours, minutes and seconds into a method and get it to return a time formatted string which should include leading zeros.
Contains online code editor
Work out the number of days between two dates
Write a C# function to work out the difference between two dates and return the number of days in this coding challenge.
Contains online code editor