无法使用System.Data.Entity.Core.Objects.ObjectQuery类型的实例调用System.Data.Entity.DbSet

时间:2015-07-03 15:59:37

标签: c# asp.net-mvc

尝试调用列表方法时出现以下错误

其他信息:方法' GRCWebApp.Models.Day Find(System.Object [])'声明类型' System.Data.Entity.DbSet 1[GRCWebApp.Models.Day]' cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery 1 [GRCWebApp.Models.Day]'

控制器

        public ActionResult ListClubMembershipType(int clubId)
    {
        //Populate the list
        var types = from s in db.MembershipTypes
                    where (s.ClubId == clubId)
                    orderby s.Type
                    select s;
        ViewBag.typesCount = types.Count();
        var model = types.Select(t => new ListMembershipTypeViewModel            
    {
        Type = t.Type,
        ClubId = clubId,
        Cost = t.Cost,
        ReducedCost = t.ReducedCost,
        ReducedDate = db.Days.Find(t.DayId).DayNum + "/" + db.Months.Find(t.MonthId).MonthName,
        MinAge = t.MinAge,
        MaxAge = t.MaxAge,
    });

        return PartialView("_ListClubMembershipType", model);
    }

,错误发生在ReducedDate行

日模型是:

public class Day
{
    public int DayId { get; set; }

    [Column(TypeName = "int")]
    public int DayNum { get; set; }

    public virtual ICollection<MembershipType> MembershipTypes { get; set; }
}

月份模型是:

    public class Month
{
    public int MonthId { get; set; }

    [Column(TypeName = "nvarchar")]
    public String MonthName { get; set; }

    public virtual ICollection<MembershipType> MembershipTypes { get; set; }
}

MembershipType模型是:

        public int MembershipTypeId { get; set; }

    [StringLength(150)]
    [Column(TypeName = "nvarchar")]
    public String Type { get; set; }

    [StringLength(350)]
    [Column(TypeName = "nvarchar")]
    public String Description { get; set; }

    [Column(TypeName = "int")]
    public int ClubId { get; set; }

    [Column(TypeName = "decimal")]
    public Decimal Cost { get; set; }

    [Column(TypeName = "decimal")]
    public Decimal ReducedCost { get; set; }

    [Column(TypeName = "int")]
    public int? DayId { get; set; }

    [Column(TypeName = "int")]
    public int? MonthId { get; set; }

    [Column(TypeName = "int")]
    public int MinAge { get; set; }

    [Column(TypeName = "int")]
    public int MaxAge { get; set; }

    [Column(TypeName = "bit")]
    public bool? Dormant { get; set; }
}

ViewModel Is:

    public class ListMembershipTypeViewModel
{
    [Display(Name = "Type")]
    public String Type { get; set; }

    public int ClubId { get; set; }

    [Display(Name = "Full Cost")]
    public Decimal Cost { get; set; }

    [Display(Name = "Reduced Cost")]
    public Decimal ReducedCost { get; set; }

    [Display(Name = "Reduced From")]
    public string ReducedDate { get; set; }

    [Display(Name = "Min Age")]
    public int MinAge { get; set; }

    [Display(Name = "Max Age")]
    public int MaxAge { get; set; }
}

我试图将其他两个表中的值组合起来,组成一个显示日期编号和月份字的字符串

我尝试添加ToString,但会产生相同的错误

1 个答案:

答案 0 :(得分:0)

我已通过将“日期和月”添加到MembershipType模型

来解决此问题
    public Day Day { get; set; }

    public Month Month { get; set; }

然后控制器中的以下内容:

ReducedDate = t.Day.DayNum + " " + t.Month.MonthName,

正如这个问题Populating List with related information

所指出的那样