将表添加到Membership-database

时间:2015-02-26 12:34:10

标签: c# sql asp.net-membership

我正在使用成员资格数据库进行用户管理。 布局如下:

enter image description here

如您所见,我添加了一个新表“下载”。 在此表中,我想存储一些其他信息。

我上课了:

public class DownloadInformation
{
    public int Id { get; set;}
    public int UserId { get; set;}
    public string UserName { get; set;}
    public string Filename { get; set;}
    public string UserIpAddress { get; set;}
    public DateTime DownloadDate { get; set; }
}

在我的ApplicationDbContext中,我做了以下事情:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {

        base.OnModelCreating(modelBuilder);

        var idUser = modelBuilder.Entity<IdentityUser>().ToTable("UserView");
        idUser.Property(p => p.Id).HasColumnName("UserId");
        idUser.Property(p => p.PasswordHash).HasColumnName("Password");
        idUser.Property(p => p.SecurityStamp).HasColumnName("PasswordSalt");
        idUser.Property(p => p.UserName).HasColumnName("UserName");

        var appUser = modelBuilder.Entity<ApplicationUser>().ToTable("UserView");
        appUser.Property(p => p.Id).HasColumnName("UserId");
        appUser.Property(p => p.PasswordHash).HasColumnName("Password");
        appUser.Property(p => p.SecurityStamp).HasColumnName("PasswordSalt");
        appUser.Property(p => p.UserName).HasColumnName("UserName");
        appUser.Property(p => p.Email).HasColumnName("Email");


        var download = modelBuilder.Entity<DownloadInformation>().ToTable("Downloads");
        download.Property(p => p.Id).HasColumnName("Id");
        download.Property(p => p.UserId).HasColumnName("UserId");
        download.Property(p => p.UserIpAddress).HasColumnName("UserIpAddress");
        download.Property(p => p.UserName).HasColumnName("UserName");
        download.Property(p => p.Filename).HasColumnName("Filename");
        download.Property(p => p.DownloadDate).HasColumnName("DownloadDate");
    }
}

但是当我尝试执行以下操作时:

using (ApplicationDbContext app = new ApplicationDbContext())
        {
            DownloadInformation downloadInfo = new DownloadInformation() { UserId = user.UserId, UserName = user.UserName, UserIpAddress = user.IpAddress, Filename = document.FilePath, DownloadDate = DateTime.Now };

            app.Download.Add(downloadInfo);
            app.SaveChanges();
        }

编译时,我收到以下错误:

'CustomerPortalDomain.Entities.ApplicationDbContext'不包含“下载”的定义,也没有扩展方法“下载”接受类型为“CustomerPortalDomain.Entities.ApplicationDbContext”的第一个参数(您是否缺少using指令或汇编参考?)

我忘记了什么?我错过了什么?

1 个答案:

答案 0 :(得分:2)

看起来您没有向ApplicationDbContext添加任何“下载”属性

public DbSet<DownloadInformation> Download{ get; set; }
相关问题