【问题标题】:How to join two or more tables using Entity Framework in ASP.NET MVC?如何在 ASP.NET MVC 中使用实体框架连接两个或多个表?
【发布时间】:2021-07-08 12:28:45
【问题描述】:

我希望使用实体框架、依赖注入和 SQLite 将 3 个表连接在一起。

到目前为止,我有这个:

using System.Collections.Generic;
using System.Linq;

namespace MMS.Data.Models
{   
    public class User
    {           
        public int Id { get; set; }

        //Role of user

        public Role Role { get; set; }
        
        // first name of user
        public string Name { get; set; }
        
        // Date of birth 
        public string DOB { get; set; }

        public string Gender { get; set; }   
        public string Address { get; set; }

        //Telephone number
        public string MobileNumber { get; set; }

        //email address
        public string EmailAddress { get; set;}

        // EF Relationship - a user can have many bookings 
        public IList<Booking> Bookings { get; set; } = new List<Booking>();
    }

    public class Booking
    {     
        public int Id { get; set; }      

        // name of reviewer
        public string Name { get; set; }   

        // date review was made        
        public DateTime CreatedOn { get; set; }

        // reviewer comments
        public string Comment { get; set; }
    
        // EF dependent relationship booking belongs to a user
        public int UserId { get; set; }

        // Navigation property
        public User User { get; set; } 
    }
}

就服务文件夹而言,我可以接受。困扰我的是如何创建第三个表模型并将其与其他两个模型相结合。

例如,我希望用户能够进行预订,并且管理员能够进来查看和添加用户、删除预订等。我理解这一点。

我如何联系员工以查看预订和人员等?我希望我正确地解释了这一点。本质上是第三个表,可以在其中创建员工并提取用户和预订信息?

我知道从技术上讲,我可以在没有第三张桌子的情况下做到这一点,因为我可以继续并允许用户访问所有预订和用户信息。但是,我确实希望某些类型的员工只能看到他们需要看到的内容。

谁能帮忙?

【问题讨论】:

  • 你在 User 模型中使用 Role 属性是为了什么?
  • @MansurKurtov 这个角色会选择某个员工,所以在顶部我会创建一个公共枚举角色 {然后插入诸如经理之类的东西}
  • 如果“角色”和“预订”模型相互绑定会怎样?
  • @MansurKurtov 抱歉,您能否通过相互绑定更好地解释一下。你的意思是我可以创建一些我可以询问员工或员工姓名然后将其绑定到预订的东西吗?如果是这样,我该怎么做?
  • 如果您从 User 模型中删除 Booking List 并将其添加到 Role 模型中,那么您可以使用它来控制用户

标签: c# asp.net-mvc sqlite entity-framework


【解决方案1】:

这样员工将被绑定到用户表。您可以访问进行预订的员工,也可以直接修改。

基本上public IList&lt;YourModel&gt; Model {get;set;} 需要在YourModel 中注解为public YourModel YourModel{get;set;} 下面是一个例子:

 public class Employee
    {
      public string Role { get; set;} // any property
      public IList<User> Users { get; set;}
    
    
    }
 public class User
    {           
        public int Id { get; set; }

        //Role of user

        public Role Role { get; set; }
        
        // first name of user
        public string Name { get; set; }
        
        // Date of birth 
        public string DOB { get; set; }

        public string Gender { get; set; }   
        public string Address { get; set; }

        //Telephone number
        public string MobileNumber { get; set; }

        //email address
        public string EmailAddress { get; set;}

        // EF Relationship - a user can have many bookings 
        public IList<Booking> Bookings { get; set; } = new List<Booking>();

        public Employee Employee { get; set; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    相关资源
    最近更新 更多