【问题标题】:how and where to add a more specific join of two entity tables如何以及在何处添加两个实体表的更具体的连接
【发布时间】:2020-05-28 21:53:40
【问题描述】:

我正在尝试为我们的公司构建一个我认为很简单的目录。我不是 Web 开发人员,我是独立的应用程序开发人员,因此我对 ASP.NET MVC 和实体框架的了解有限。

我按照我在网上找到的一些说明在旧应用程序中创建了实体(2 个表),用于获取员工列表的 SQL 语句使用左外连接和 3 个 where 参数。我假设我必须在 model.Context.vb 文件中使用 LINQ?

我还在控制器中看到Details 方法,但它创建了这个上下文文件的一个实例,所以我假设这是我需要组合两个实体然后通过控制器访问优化列表的地方,这是对的吗?如果我什至需要这样做,我将如何以及在哪里编写 LINQ 语句?

这是控制器:

Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Entity
Imports System.Linq
Imports System.Net
Imports System.Web
Imports System.Web.Mvc
Imports MillDirectory

Namespace Controllers
Public Class tblUsersController
    Inherits System.Web.Mvc.Controller

    Private db As New HDDataEntities

    ' GET: tblUsers
    Function Index() As ActionResult
        Return View(db.tblUsers.ToList())
    End Function

    ' GET: tblUsers/Details/5
    Function Details(ByVal id As String) As ActionResult
        If IsNothing(id) Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim tblUser As tblUser = db.tblUsers.Find(id)
        If IsNothing(tblUser) Then
            Return HttpNotFound()
        End If
        Return View(tblUser)
    End Function

    ' GET: tblUsers/Create
    Function Create() As ActionResult
        Return View()
    End Function

    ' POST: tblUsers/Create
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Create(<Bind(Include:="USLanID,USAS400ID,USVaxID,USLName,USFname,USDepartment,USPhoneNumber,USFaxNumber,USExtension,USPagerNumber,USSupervisor,USPhoneAreaCode,USPagerAreaCode,USFaxAreaCode,USHelpDeskFlag,USAvailable,USAvailableDate,USEmail,USDomain,USInactive,KICKUser,KICKMinutes,USNewTicketNotify,USNAME,USClosedTicketnotify,USCellAreaCode,USCellPhoneNumber,USAllowedToPage,USLocation,USHomeArea,USHomePhone,USExcludeHomePhone,USEmpNum,USCellTxtMsgAddress,USHireDate,USTrackVac,USIncludeinCellDownload,USRecoveryMsg,USPowerMsg,USPMBreakMsg,USProdRPT,USStockRPT,USPagingType,USPicturePath,USTest,USPMBreakMins,USPMBreakNum,USmsgPCName")> ByVal tblUser As tblUser) As ActionResult
        If ModelState.IsValid Then
            db.tblUsers.Add(tblUser)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(tblUser)
    End Function

    ' GET: tblUsers/Edit/5
    Function Edit(ByVal id As String) As ActionResult
        If IsNothing(id) Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim tblUser As tblUser = db.tblUsers.Find(id)
        If IsNothing(tblUser) Then
            Return HttpNotFound()
        End If
        Return View(tblUser)
    End Function

    ' POST: tblUsers/Edit/5
    'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    'more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    <HttpPost()>
    <ValidateAntiForgeryToken()>
    Function Edit(<Bind(Include:="USLanID,USAS400ID,USVaxID,USLName,USFname,USDepartment,USPhoneNumber,USFaxNumber,USExtension,USPagerNumber,USSupervisor,USPhoneAreaCode,USPagerAreaCode,USFaxAreaCode,USHelpDeskFlag,USAvailable,USAvailableDate,USEmail,USDomain,USInactive,KICKUser,KICKMinutes,USNewTicketNotify,USNAME,USClosedTicketnotify,USCellAreaCode,USCellPhoneNumber,USAllowedToPage,USLocation,USHomeArea,USHomePhone,USExcludeHomePhone,USEmpNum,USCellTxtMsgAddress,USHireDate,USTrackVac,USIncludeinCellDownload,USRecoveryMsg,USPowerMsg,USPMBreakMsg,USProdRPT,USStockRPT,USPagingType,USPicturePath,USTest,USPMBreakMins,USPMBreakNum,USmsgPCName")> ByVal tblUser As tblUser) As ActionResult
        If ModelState.IsValid Then
            db.Entry(tblUser).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(tblUser)
    End Function

    ' GET: tblUsers/Delete/5
    Function Delete(ByVal id As String) As ActionResult
        If IsNothing(id) Then
            Return New HttpStatusCodeResult(HttpStatusCode.BadRequest)
        End If
        Dim tblUser As tblUser = db.tblUsers.Find(id)
        If IsNothing(tblUser) Then
            Return HttpNotFound()
        End If
        Return View(tblUser)
    End Function

    ' POST: tblUsers/Delete/5
    <HttpPost()>
    <ActionName("Delete")>
    <ValidateAntiForgeryToken()>
    Function DeleteConfirmed(ByVal id As String) As ActionResult
        Dim tblUser As tblUser = db.tblUsers.Find(id)
        db.tblUsers.Remove(tblUser)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If (disposing) Then
            db.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
End Class
End Namespace

【问题讨论】:

    标签: sql asp.net-mvc vb.net entity-framework


    【解决方案1】:

    方法一:可以通过在Controller中添加这个来拉取结果集。我已经编辑了索引操作:

    Function Index() As ActionResult
        var query = from u in db.tblUser
                    join p in db.tblUserPermission on u.UserID equals p.UserID
                    where u.YourProperty == YourCondition 
                    into gj
                    from x in gj.DefaultIfEmpty()
                    select new { 
                     UserID = u.UsergroupID,
                     UserPermission = p.UserPermission               
                    };
        Return View(query)
    End Function
    

    方法二:

    Function Index() As ActionResult
        Return View(db.UserDbSet.UserPermissionList.Where(w => w.YourProperty == 
                    w.YourFirstCond && w.YourAnotherProp == w.YourSecondCond).ToList())        
    End Function
    

    假设左表的 DbSet 是 UserDbSet。 在此之前,您需要根据您的关系更改模型(实体)。我已经给出了一对多关系的场景。

    //In tblUser Model
    public List<tblUserPermission> UserPermissionList {get;set;}
    
    //In tblUserPermission model
    public tblUser User {get;set;}
    

    抱歉,模型和第一种方法在 C# 代码中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2017-01-29
      • 2014-01-02
      • 1970-01-01
      • 2011-08-25
      相关资源
      最近更新 更多