我有一个webforms应用程序,它调用一个服务然后使用EF来获取一个对象。我在应用程序运行时添加了一个种子例程来填充我的数据库,但WCF上的登录函数不会返回结果。我认为这是因为我的种子方法没有运行,我错过了什么?
上下文
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;
}
}
迁移配置
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服务(通过我的网络表单中的按钮点击调用)
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;
}
}