【问题标题】:Adding a table to the Membership-database将表添加到 Membership-database
【发布时间】:2015-02-26 12:34:09
【问题描述】:

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

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

我上课了:

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”不包含“Download”的定义,并且找不到接受“CustomerPortalDomain.Entities.ApplicationDbContext”类型的第一个参数的扩展方法“Download”(您是否缺少 using 指令或程序集参考?)

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

【问题讨论】:

    标签: c# sql asp.net-membership


    【解决方案1】:

    您似乎没有向 ApplicationDbContext 添加任何“下载”属性

    public DbSet<DownloadInformation> Download{ get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 2010-12-05
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多