We can unit test Entity Framework Code First using Effort.
Effort can be installed in test project using nuget package https://www.nuget.org/packages/Effort.EF6/
Following below steps we can have basic EF code first unit test.
Create Student Domain Class
public class Student
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int CollegeId { get; set; }
public DateTime DOB { get; set; }
}
Create Mapping Class
public class StudentMap : EntityTypeConfiguration
{
public StudentMap()
{
this.ToTable("Student");
this.HasKey(s => s.Id);
this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(s => s.Email).IsRequired().HasMaxLength(50);
this.Property(s => s.FirstName).IsRequired().HasMaxLength(50);
this.Property(s => s.LastName).IsRequired().HasMaxLength(50);
this.Property(s => s.DOB).IsRequired();
}
}
Create School Context Class
Note: There is DatabaseInitializer which is drop and create always required for unit testing.
public class SchoolContext : DbContext
{
public SchoolContext() : base()
{
}
/// /// We need this constructor to pass the Effort dbConnection to process data in memory
/// public SchoolContext(DbConnection dbConnection) : base(dbConnection, true)
{
Database.SetInitializer(
new DropCreateDatabaseAlways());
}
public DbSet Student { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new StudentMap());
base.OnModelCreating(modelBuilder);
}
}
Create StudentRepository and Student Service class to perform Insert/Update Operations.
IStudentRepository and StudentRepository
public interface IStudentRepository
{
Student GetStudent(int studentId);
int AddStudent(Student student);
}
public class StudentRepository : IStudentRepository
{
private SchoolContext context;
public StudentRepository(SchoolContext context)
{
this.context = context;
}
public Student GetStudent(int studentId)
{
return this.context.Student.Find(studentId);
}
public int AddStudent(Student student)
{
this.context.Student.Add(student);
this.context.SaveChanges();
return student.Id;
}
}
IStudentService and StudentService
public interface IStudentService
{
Student GetStudent(int studentId);
int AddStudent(Student student);
}
public class StudentService : IStudentService
{
private IStudentRepository _iStudentRepository;
public StudentService(IStudentRepository iStudentRepository)
{
_iStudentRepository = iStudentRepository;
}
public Student GetStudent(int studentId)
{
return _iStudentRepository.GetStudent(studentId);
}
public int AddStudent(Student student)
{
return _iStudentRepository.AddStudent(student);
}
}
Unit Test Class
Add Effort.EF6 Nuget package to unit test library
[TestClass]
public class EntityFrameworkTest
{
private SchoolContext context;
[TestInitialize]
public void Initialize()
{
context = new SchoolContext(Effort.DbConnectionFactory.CreateTransient());
}
[TestMethod]
public void TestConnection()
{
IStudentService studentService = new StudentService(new StudentRepository(context));
studentService.AddStudent(new Student()
{
FirstName = "Murtaza",
LastName = "Ali",
Email = "murtaza@great",
DOB = DateTime.Now,
CollegeId = 75
});
var student = studentService.GetStudent(1);
}
}
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. Devops Training in Chennai
ReplyDeleteDevops Training Institute in Chennai
Very good blog, thanks for sharing such a wonderful blog with us. Keep sharing such worthy information to my vision.
ReplyDeleteDevOps certification Chennai
DevOps Training in Chennai
DevOps Training in Velachery
RPA Training in Chennai
Angularjs Training in Chennai
Blue Prism Training in Chennai
Machine Learning Training in Chennai
Thanks. Glad it helped you in anyway.
ReplyDeleteInteresting Post. Looking for this information for a while. Thanks for Posting.
ReplyDeleteNode JS Training Institute in chennai
Node JS Training Institutes in chennai
Node JS Course
Node JS Training in Velachery
Node JS Training in Tambaram
Node JS Training in Adyar
More informative,thanks for sharing with us.
ReplyDeletethis blog makes the readers more enjoyable.keep add more info on your page.
vmware Certification Training in T nagar
vmware Courses in T nagar
vmware training in bangalore
vmware training institutes in bangalore
Thanks for your sharing the knowledge to needed ones. Waiting for your updates. Keep it up.
ReplyDeleteDigital Marketing Classes in Bangalore
Best Digital Marketing Course in Bangalore
Digital Marketing Course in Bangalore
Digital Marketing Training in Tnagar
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Course in Omr
ReplyDeleteThanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
Best Web Designing Institute in Chennai
Best web designing course in chennai
web design classes
PHP Training Center in Chennai
PHP Institutes in Chennai
PHP courses in chennai
great idea you have enclosed in your blog.i have ever seen such kind of information's. it is really useful for every readers who will read it.
ReplyDeleteBest Software Testing Training Institute in Anna nagar
Software Testing Training in Ambattur
Software Testing Certification Training in T nagar
Software Testing Courses in T nagar
Software Testing Training in OMR
Software Testing Training in Sholinganallur
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
ReplyDeleteGood discussion.
Android Training Chennai
Android Courses in Chennai
Android Mobile apps Development Training in Chennai
AWS Training in Chennai
AWS Training
AWS Course in Chennai
Brilliant ideas that you have share with us for, RPA Training in Chennai
ReplyDeleteVery useful and clearly explained
ReplyDeleteRegards
Machine Learning Institute
The blog you have shared is more informative... Thanks for your valid blog.
ReplyDeleteSelenium Training in Bangalore
Selenium Training in Coimbatore
Selenium Training Institutes in Bangalore
Ethical Hacking Course in Bangalore
German Classes in Bangalore
German Classes in Madurai
Hacking Course in Coimbatore
German Classes in Coimbatore
You are an artist. Your website is very unique. Your content is very interesting to read. Waiting to read more.
ReplyDeleteInformatica Training in Chennai
Informatica Training Center Chennai
Informatica Training
Learn Informatica
Informatica course
Informatica Training in Velachery
Informatica Training in Anna Nagar
Informatica Training in Tnagar
ReplyDeleteHello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
Android Training in Chennai | Best Android Training in Chennai
Data Science Training in Chennai | Best Data Science Training in Chennai
Matlab Training in Chennai | Best Matlab Training in Chennai
Best AWS Training in Chennai | AWS Training in Chennai
Selenium Training in Chennai | Best Selenium Training in chennai
Devops Course Training in Chennai | Best Devops Training in Chennai
Its a wonderful post and very helpful, thanks for all this information.
ReplyDeleteVmware Training institute in Noida
Great Work. Amazing way of writing things. Thanks for Posting.
ReplyDeleteIonic Training in Chennai
Ionic Course in Chennai
Ionic Training Course
Ionic Framework Training
Ionic Course
Ionic Training near me
Ionic Training in Velachery
Ionic Training in Tambaram
Ionic Training in Adyar
Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live.
ReplyDeleteangularjs training in chennai | angularjs course in chennai | angularjs training institute in chennai | angularjs training institutes in chennai
It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
ReplyDeleteDatascience Course in Chennai | Datascience Training in Chennai
I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
ReplyDeleteBest PHP Training Institute in Chennai|PHP Course in chennai
Best .Net Training Institute in Chennai
Matlab Training in Chennai
Embedded Training in Chennai
SAS Training in Chennai
It has been great for me to read such great information about vmware.vmware training in bangalore
ReplyDeleteThe way of Explanation's about the content is ease to understand...Thanks for such a clear flow of content...Keep Doing this amazing works
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
Great article. keep on doing just an excellent blog.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
I find this blog to be very interesting and very resourceful. I would say that your blogs are really interesting and informative for me and this article explained everything in detail.
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training in Tambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
This comment has been removed by the author.
ReplyDeleteThanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging.
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Velachery
Software Testing Training in Tambaram
Software Testing Training in Porur
Software Testing Training in Omr
Software Testing Training in Annanagar
Interesting blog, it gives lots of information to me. Thanks for sharing such a nice blog.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital Marketing Training in Annanagar