0

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;
    } 
}
floormind
  • 1,868
  • 5
  • 31
  • 85
  • Have you run update-database ? – Steve Greene Jun 03 '15 at 17:39
  • @SteveGreene i get this error No migrations configuration type was found in the assembly 'Services' when i try to run that – floormind Jun 03 '15 at 17:57
  • OK, your Seed won't run until the migration does. Is that the project with your migrations? Might need to change the default project dropdown in packet manager. Otherwise add the ContextProjectName switch when enabling migrations. http://stackoverflow.com/questions/18126711/enable-migrations-with-context-in-separate-assembly – Steve Greene Jun 03 '15 at 18:56
  • My migration is in my DataAccessLayer project, there is a folder called "Migrations" and a file in it called Configuration.cs, this is there because i have already ran the Enable migration on that DataAccessLayer project. @SteveGreene – floormind Jun 03 '15 at 19:22
  • OK, you might need to specify the project when you run update-database or change the default database in packet manager console. http://coding.abel.nu/2012/03/ef-migrations-command-reference/#Update-Database – Steve Greene Jun 03 '15 at 19:31

0 Answers0