Part 6 How to seed database with test data using entity framework

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

Entity Framework - All Text Articles

Entity Framework - All Slides

Entity Framework Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

So far in this video series, we have been manually populating the database with test data using the insert sql script. Entity Framework can automate this. We will be working with the example we worked with in Part 5. Here are the steps.

using System.Collections.Generic;
using System.Data.Entity;

namespace Demo
{
public class EmployeeDBContextSeeder : DropCreateDatabaseIfModelChanges[EmployeeDBContext]
{
protected override void Seed(EmployeeDBContext context)
{
Department department1 = new Department()
{
Name = "IT",
Location = "New York",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Mark",
LastName = "Hastings",
Gender = "Male",
Salary = 60000,
JobTitle = "Developer"
},
new Employee()
{
FirstName = "Ben",
LastName = "Hoskins",
Gender = "Male",
Salary = 70000,
JobTitle = "Sr. Developer"
},
}
};

Department department2 = new Department()
{
Name = "HR",
Location = "London",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Philip",
LastName = "Hastings",
Gender = "Male",
Salary = 45000,
JobTitle = "Recruiter"
},
}
};
Department department3 = new Department()
{
Name = "Payroll",
Location = "Sydney",
Employees = new List[Employee]()
{
new Employee()
{
FirstName = "Steve",
LastName = "Pound",
Gender = "Male",
Salary = 45000,
JobTitle = "Sr. Payroll Admin",
},
}
};

context.Departments.Add(department1);
context.Departments.Add(department2);
context.Departments.Add(department3);

base.Seed(context);
}
}
}

Database.SetInitializer(new EmployeeDBContextSeeder());

[Table("tblEmployees")]
[Column("First_Name")]

At this point Employee class should look as shown below.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; }
[ForeignKey("DepartmentId")]
public Department Department { get; set; }
public string JobTitle { get; set; }
}

Step 5: Run the application and notice that the Sample database, Departments and Employees tables are created and populated with test data automatically.
Рекомендации по теме
Комментарии
Автор

Thank you Thank you Thank you!! I've watched almost all the series you have offered and you are very clear and concise! This by far is more informative than any other EF resource I have found thus far.

bytesizedpieces
Автор

Thank you very much Sir. Currently I'm working on a Silverlight application which utilizes MongoDB. Most companies however preferably use SqlServer and EntityFramework becomes a value added knowledge when it comes to interviews. Appreciate you taking time for the knowledge transfer.

gokulsukumar
Автор

thank you very much for this series! and other

melkamuademe
Автор

Hi Venkat, there's no doubt about all your video uploads they're absolutely excellent.Pls can you help to upload a C# FileSystem or Directory watcher and another one on how to publish an application.I started learning programming with your videos and i must say a big thumbs up! Thanks

yisauramon
Автор

I followed your instruction and it worked great. but i have questions. How about creating Department and Employee separately? (your example creates both at the same time)

jumbo
Автор

hei guys, the code works, you should just first comment the public List<Department> GetDepartments() method, otherwise you get an error, first when the database dropped and created then you can call the Method and you can add many property as you want and many BoundField as you want, but dont forget the code below has some failure it uses [ ] instead < >

aliajdadi
Автор

If your code doesn't work, it's because here seeder class is inheriting from 'DropCreateDatabaseIfModelChanges', so if you don't change your entity class each time in any form e.g. adding new property like 'JobTitle', it won't recreate the database.
So to solve the problem you can make seeder class inherit from 'DropCreateDatabaseAlways' instead.

debarghyachakraborty
Автор

please make us a tutorial about
Deploying Windows app and Windows Server 2012 R2 using MDT
Printing and the most its most important options
thank you  very much for this series!

krimbelkacem
Автор

Thank you very for starting another great series. Before starting Entity framework i googled and found some limitation in it. If possible i would like to request you to record one video of entity framework  pros and cons. 

ramchandrathakkar
Автор

I did creat a database but l will check this video if it works to seed the database

alialrahem
Автор

hi
what about migration model. or how i can add column to existing table. without losing the data.

ferasighbaria
Автор

This is way to add just  one column in database(Every time  change the model) .Now  model  changes behind the scene  Entity totally drop the database  and create new one .if my database have some data then i want just add on additional column to that table without delete the existing record and just alter my table with new column ....how  do it  =>Thank You

mageshwarank
Автор

Here, whichever test data you have added it is displaying as it is in  y application too. but then problem is when I add a new employee nothing happen.Why?

shailendraphadke
Автор

Please Sir Upload Linq Video Tutorial.

vinoddhekane
Автор

Hello Sir, In real time Projects/applications which Approach is the best?
1. Simple EF, 2. POCO(model), 3. Code First ??
Please Let me know.
My DB containing number of Procedures and Views.

cherryt
Автор

(Entity FrameWork Part-6) If we don't make changes in Model(i.e either in department or employee) and try to run the program by making modifications in EmployeeDBContextSeeder class it will run successfully but the changes which are done in EmployeeDBContextSeeder will not be reflected in the browser. So need your hep in understanding this concept of Data-Migration History and why those changes in EmployeeDBContextSeeder class are not reflected in the browser. Thank you in advance

kiranh
Автор

hello sir i am getting an error.please tell me what is the problem here.
error:

Server Error in '/' Application.

A field or property with the name 'userCity1' was not found on the selected data source.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A field or property with the name 'userCity1' was not found on the selected data source.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

here is used UserCity table that contain id and userCity Name, and tblUser which contain FirstName, LastName, Gender etc,

My DB is getting populated but still i am getting such error.please help ASAP

mohitsingh
Автор

Hi, Can anyone tell me how can we seed huge number (say 1000) of tables with 1000 records each?

tech-with-inder
Автор

please provide LINQ videos 

Thank you.

srikanthkalyan
Автор

how we overwrite an existing migration file

poojabolwade