【问题标题】:Data access using Entity Framework in ASP.NET MVC在 ASP.NET MVC 中使用实体框架进行数据访问
【发布时间】:2013-09-20 19:06:36
【问题描述】:

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

表格

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

PlotXY.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 值。 此外,该表没有唯一列,没有空值,这里不需要更新任何内容。我只想做一个选择。

【问题讨论】:

  • 我不知道我是否理解正确。在我看来,正确的工作查询返回您的 ORM 类的实例,其中 Twnshp 为 1,Rng 为 4,Section 为 31。
  • 当我调试并查看“查询”时,它会抛出这个错误“从物化 'System.String' 类型到 'System.Int32' 类型的指定转换无效。'
  • 哪家生产线?
  • 没有编译错误
  • 让我重新表述一下,我尝试调试并检查查询是否返回任何内容,结果在“var query”上发现了此异常。

标签: c# sql-server asp.net-mvc linq entity-framework


【解决方案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();
        }
    }   

【讨论】:

【解决方案2】:

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

感谢大家的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多