无法将WinForms应用程序与ASP.NET MVC应用程序数据库连接?

时间:2016-11-26 19:12:49

标签: c# asp.net-mvc winforms entity-framework ef-code-first

我将共享类放在一个类库中,并在两个项目中引用它,这些是我在类库中使用的类:

namespace SharedModels
{
    public class ApplicationUser : IdentityUser
    {
        public ICollection<UserTask> Tasks { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    [Table("Task")]
    public class UserTask
    {
        public int Id { get; set; }
        [Required, MaxLength(100)]
        public string Title { get; set; }
        public string Description { get; set; }
        [DataType(DataType.Date)]
        public DateTime DueDate { get; set; }
        public string Sataus { get; set; }
        public ICollection<ApplicationUser> Users { get; set; }
    }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<UserTask> Tasks { get; set; }

        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityRole>().ToTable("Role");
            modelBuilder.Entity<ApplicationUser>().ToTable("User");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
            modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");

            modelBuilder.Entity<ApplicationUser>()
                .HasMany(u => u.Tasks)
                .WithMany(t => t.Users)
                .Map(x =>
                {
                    x.ToTable("UserTask");
                    x.MapLeftKey("UserId");
                    x.MapRightKey("TaskId");
                });
        }
    }

}

在这个课程中,我使用了两个应用程序中需要的普通成员, MVC应用程序工作正常,但WinForms似乎没有看到数据库从中读取,没有错误。 我有一个从数据库中填充的listBox:

listBoxUsers.DataSource = ctx.Users.ToList();

但它返回null。 此方法也返回null,我在登录表单中使用它:

public async Task<bool> VerifyUserNamePassword(string userName, string password)
{
    var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
    return await usermanager.FindAsync(userName, password) != null;
}

我无法弄清楚发生了什么,在类库之前我在两个应用程序中为同一个实体使用了两个单独的EF代码第一个模型并且它很乱,但应用程序正在运行!

0 个答案:

没有答案