0

我有一个包含这些列(类型)的表,如下所述。

桌子

------------------------------------------------------------------
Dir(str) | Twnshp(int) | Rng(int) | Section(int) | Xcell(int) | Ycell(int)
------------------------------------------------------------------

我正在尝试使用 EF 进行此查询。

SELECT Xcell,Ycell  FROM [CIR].[dbo].[TRS2Cell] where Twnshp = 1 and Rng = 4 and Section =31

经过一番研究,我创建了一个 DAL 上下文和类,如下所示。

PlotXYContext.cs

public class PlotXYContext :DbContext
{
    public DbSet<PlotXY> XYCells { get; set; }
}

绘图XY.cs

[Table("TRS2Cell")]
public class PlotXY
{

    public string Dir { get; set; }
    [Key]
    public int Twnshp { get; set; }
    public int Rng { get; set; }
    public int Section { get; set; }
    public int Xcell { get; set; }
    public int Ycell { get; set; }
}

这是我的控制器中的代码,我在其中传递了三个参数。

PlotXYContext plotXYContext = new PlotXYContext();
        var query = from TRS2Cell in plotXYContext.XYCells
                    where TRS2Cell.Twnshp == 1
                    && TRS2Cell.Rng == 4
                    && TRS2Cell.Section == 31
                    select TRS2Cell.Xcell;

我需要 EF 方面的帮助,因为我是新手,这也是正确的查询吗?如果是这样,我如何从查询中检索 Xcell 和 Ycell 值。此外,该表没有唯一列,没有空值,这里不需要更新任何内容。我只想做一个选择。

4

2 回答 2

1

通常你不想在你的控制器中做任何数据访问代码。你想把它们分开。此外,当我第一次开始使用 EF 时,当我开始使用 MVC 时,我也被 DB Context 挂断了。如果您正确添加了 Ado.Net 实体数据模型,则应自动为您创建 db 上下文。如果您查看“Entity”.edmx =>“Entity”.Context.tt 下的“YourEntity”.cs 文件,它将看起来像

public partial class VuittonEntities : DbContext
{
    public VuittonEntities()
        : base("name=VuittonEntities")
    {
    }

为了帮助您解决 EF,我将发布我所有的代码以供查询。

所以模型文件夹中的模型类将如下所示。

public class RoleGridViewModel
{
    public int UserID       { get; set; }
    public string UserFirst    { get; set; }
    public string UserLast     { get; set; }
    public string UserRole     { get; set; }
    public string UserRoleDesc { get; set; }
}

这是您的数据访问层功能:这里我正在创建我的模型类的列表,因为稍后我将在 gridview 中填充它。

 public List<RoleGridViewModel> GridRoles()
    {
        using (VuittonEntities db = new VuittonEntities())
        {
            return (from users in db.User
                    join roles in db.UserRole on users.RoleID equals roles.RoleID
                    select new RoleGridViewModel
                    {
                        UserID = users.UserID,
                        UserFirst = users.FirstName,
                        UserLast = users.LastName,
                        UserRole = roles.Role,
                        UserRoleDesc = roles.Role_Desc
                    }).ToList();

        }
    }

在您的控制器中,您可以这样称呼它。通常你会从你的控制器调用一个 businezz 层,我会直接进入数据层来向你展示它是如何完成的。这里 var roles 保存您的查询。我在这里使用 Json 结果,但这也可以在操作结果中完成

公共 JsonResult RolesGrid() {

        var roles = new UserDAL().GridRoles();

        return Json(roles, JsonRequestBehavior.AllowGet);
    }

如果您只想选择一个项目,则必须在查询末尾使用 .First() ,如下所示...

  public string currentRole(UserViewModel uvm)
    {            
        using (VuittonEntities db = new VuittonEntities())
        {
            return (from us in db.User
                    join usRole in db.UserRole on us.RoleID equals usRole.RoleID
                    where (us.RoleID == uvm.RoleID) && (us.UserID == uvm.UserID)
                    select usRole.Role).First();
        }
    }   
于 2013-09-20T19:48:28.173 回答
0

我发现我没有使用与表中类似的数据类型来为其声明类。Thant 是我遇到的唯一解决它的问题,因此是错误。

谢谢大家的回复。

于 2013-09-23T20:37:44.487 回答