I have a webforms application which calls a service which then uses EF to get an object. I have added a seed routine to populate my database when the application runs, but the Login function on the WCF does not return a result. I assume this is because my Seed method is not running, what I'm i missing ?
Context
public class UserTaskContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<UserTask> UserTasks { get; set; }
public UserTaskContext()
: base("Local")
{
Configuration.AutoDetectChangesEnabled = true;
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
}
Migration Configuration
internal sealed class Configuration : DbMigrationsConfiguration<TODO.DataAccessLayer.UserTaskContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(TODO.DataAccessLayer.UserTaskContext context)
{
context.Users.Add(new User { Id = Guid.NewGuid(), Password = "password", Username = "username" });
context.UserTasks.Add(new UserTask
{
Id = Guid.Parse("c9de7364-049b-4ad2-9f75-ba142fe16d1a"),
Name = "Site",
Summary = "Finish this project",
DueDate = DateTime.Now,
CreatedDate = DateTime.Now.AddDays(2),
Completed = true
});
context.SaveChanges();
}
}
WCF Service (This is called by a button click in my webforms)
public class UserService : IUserService
{
public User Login(string username, string password)
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
throw new Exception("Users name or password cannot be empty");
}
User user = null;
using (var userContext = new UserTaskContext())
{
user =
userContext.Users.FirstOrDefault(
u => u.Username.Equals(username, StringComparison.InvariantCulture) &&
u.Password.Equals(password, StringComparison.InvariantCulture));
}
return user;
}
public List<User> GetUsers()
{
List<User> users = null;
using (var context = new UserTaskContext())
{
users = context.Users.ToList();
}
return users;
}
}