【问题标题】:C# - The entity or complex type cannot be constructed in a LINQ to Entities queryC# - 无法在 LINQ to Entities 查询中构造实体或复杂类型
【发布时间】:2015-12-31 03:33:10
【问题描述】:

我正在尝试使用视图发送数据并将其打印出来,但由于我对 C# 非常陌生,因此我非常努力。

这是我的模型 ViewModel:

namespace P104.Models
{
    public class ViewModel
    {
    }

    public class Location
    {
        public int loc_id { get; set; }
        public string loc_name { get; set; }
    }

    public class Competentie
    {
        public int Comp_id { get; set; }
        public string competentie { get; set; }
    }

    public class MyViewModel
    {
        public User User { get; set; }
        public IEnumerable<Locations> Locations { get; set; }
        public IEnumerable<Competenties> Competenties { get; set; }
    }
}

这是我在控制器中的功能

public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    User user = db.user.Find(id);

    if (user == null)
    {
        return HttpNotFound();
    }

    var competenties =
        from usercomp in dbE.UserComp
        join comp in dbE.Competenties on usercomp.fk_Comp_id equals comp.comp_id
        where usercomp.fk_user_id == id
        select new Competenties { competentie = comp.competentie };

    var locations =
        from userloc in dbE.UserLoc
        join loc in dbE.Locations on userloc.fk_location_id equals loc.loc_id
        where userloc.fk_user_id == id
        select new Locations { loc_name = loc.loc_name };

    var model = new MyViewModel
    {
        User = user,
        Locations = locations.ToList(), // eagerly fetch the data that will be needed in the view
        Competenties = competenties.ToList(), // eagerly fetch the data that will be needed in the view
    };
    return View(model);
}

他就是我尝试在视图中打印出来的方式:

@foreach (var location in Model.Locations)
{
    <dt>@location.locname</dt>
    <dd>@location.locname</dd>
}
@foreach (var competentie in Model.Competenties)
{
    <dt>@competentie.competentie</dt>
    <dd>@competentie.competentie</dd>
}

我总是收到此错误

实体或复杂类型“P104.Models.Locations”不能 在 LINQ to Entities 查询中构造。

我找到了一些解决方案,但我很难将它们应用到我的代码中,因此它们不起作用。 提前感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

    你这里好像打错了:

    select new Locations { loc_name = loc.loc_name };
    

    这应该是:

    select new Location { loc_name = loc.loc_name };
    

    您要投影的模型称为Location,而不是Locations。尚不清楚 Locations 模型是什么,因为您没有在问题中表明这一点。

    当然也要相应地调整你的观点:

    @foreach (var location in Model.Locations)
    {
        <dt>@location.loc_name</dt>
        <dd>@location.loc_name</dd>
    }
    

    顺便说一句,我建议您遵循标准的 C# 命名约定。此约定规定,在 C# 中,属性名称应以大写字母开头,并且不包含 _。所以基本上你宁愿使用LocationName 或者只是Name 而不是loc_name。您的 Competentie 模型也有同样的评论。

    【讨论】:

    • 突然我得到这个错误 Invalid object name 'dbo.UserComps'。数据库被命名为“dbo.UserComp”,我不知道他从哪里得到这个。
    • 如果不知道dbo 在您的上下文中是什么或UserComp,真的很难提供帮助。我建议您提出一个新问题,并请尽量具体。仅显示代码的相关部分。在这种情况下,这将是您的 EF DataContext 定义和 LINQ 查询。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2017-05-31
    • 2015-02-06
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多