让 AspNetUsers 联接到自定义表



我正在尝试授权特定用户仅查看他自己的公司数据。我在 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中的定义">错误。我忽略了什么?我的上下文(仍然(非常标准,如下所示:

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; }

我认为问题是包含IdentityDbContext<T>上的用户的属性被称为Users而不是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而不是db.AspNetUsers发现零件(

相关内容

  • 没有找到相关文章

最新更新