[英]ASP.NET MVC customer portal User Junction Table
Im building a management portal for a chain of restaurants. 我正在为连锁餐厅建立管理门户。 I am using ASP.NET MVC with EF Code First.
我使用带有EF Code First的ASP.NET MVC。
I want each user to, after login, only see the rescources that are connected to them. 我希望每个用户在登录后只能看到与他们连接的资源清单。 I want to put a junction table(many-to-many) between ApplicationUser and the Restaurant-class(model), since each user can have/work at many restaurants, and each restaurant can have many owners/workers.
我想在ApplicationUser和Restaurant-class(模型)之间放置一个连接表(多对多),因为每个用户可以在很多餐厅工作/工作,并且每个餐厅可以有很多所有者/工作人员。
How do you do this in EF Code first? 您如何首先在EF代码中执行此操作? The same way I did Restaurant --> Menue?
我使用餐厅->菜单的方式是否相同? Do you need to build a new DBContext for Applicationuser for this to work?
您是否需要为Applicationuser构建一个新的DBContext才能起作用?
public class Restaurant
{
public int Id { get; set; }
public string Name { get; set; }
public string Adress { get; set; }
public string PhoneNumber { get; set; }
public DateTime StartDate { get; set; }
//Connections
public virtual ICollection<Menue> Menues { get; set; }
}
public class Menue
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public DateTime ModifyDate { get; set; }
//FK For RestaurantConnection
public int RestaurantId { get; set; }
}
For many to many configuration do like this 对于许多配置,请执行以下操作
Student class should have a collection navigation property for Course, and Course should have a collection navigation property for student
学生班级应具有课程的集合导航属性,课程应具有学生的集合导航属性
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public int StudentId { get; set; }
[Required]
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
In your DbContext add this configuration 在您的DbContext中添加此配置
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasMany<Course>(s => s.Courses)
.WithMany(c => c.Students)
.Map(cs =>
{
cs.MapLeftKey("StudentRefId");
cs.MapRightKey("CourseRefId");
cs.ToTable("StudentCourse");
});
}
For more information read this article Configure Many-to-Many relationship 有关更多信息,请阅读本文“ 配置多对多关系”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.