让AspNetUsers加入自定义表

时间:2017-07-04 12:52:44

标签: asp.net entity-framework linq asp.net-identity visual-studio-2017

我正在尝试授权特定用户只查看自己的公司数据。我在ApplicationUser中创建了一个名为CompanyId的自定义字段,并创建了一个CompanyController(在Erik Funkenbusch的帮助下),如下所示:

public class CompanyController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: Company
    public ActionResult Index()

{
        var companies = from c in db.Companies
                        join u in db.AspNetUsers
                        on c.CompanyID equals u.Company.Id
                        where u.UserName = User.Identity.Name
                        select c;
        return View(companies);
    }

但是:db.AspNetUsers在VS2017中的ApplicationDbContext' -error中返回'不包含定义。我在俯瞰什么?我的上下文(仍然)非常标准,看起来如下:

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

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<Company> Companies { get; set; }

1 个答案:

答案 0 :(得分:0)

我认为问题在于 SaveFileDialog svg = new SaveFileDialog(); svg.ShowDialog(); PdfWriter.GetInstance(document, new FileStream(svg.FileName + ".pdf", FileMode.Create)); 上包含用户的属性称为IdentityDbContext<T>而不是Users。见here

所以你应该试试这个:

AspNetUsers

(使用 var companies = from c in db.Companies join u in db.Users on c.CompanyID equals u.Company.Id where u.UserName == User.Identity.Name select c; 代替db.Users

发现该部分