Inserting EF Code-first seed data only once

c# code-first entity-framework-6

Accepted Answer

You may just determine whether the table is empty first:

if (!context.Users.Any())
{
    User user = new User()
    {
        UserId = 1,
        EmailAddress = "xyz@abc.com",
        LoginPassword = "123",
        CurrentBalance = 0
    };
    context.Users.AddOrUpdate(user);
}

or to check whether an entry with UserId = 1 already exists:

if (context.Users.Where(a => UserId == 1).Count() == 0) { ...
4
9/13/2017 1:17:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow