繁体   English   中英

Asp.Net - 无法创建类型的常量值。 在此上下文中仅支持基元类型或枚举类型

[英]Asp.Net - Unable to create a constant value of type. Only primitive types or enumeration types are supported in this context

这里有很多这些,但大多数涉及一个独特的问题,我还没有找到一个适用于我的代码。

当我尝试使用ToList()转换为列表时发生错误。

当我查看断点时, fields包含任何数据,所以它可能是select命令。

==.Equals()产生相同的结果。

控制器代码:

        var fields = from f in db2.Fields
                      where f.UpFile.Equals(upFile)
                      select f;
        ViewBag.Fields = fields.ToList();

型号代码:

namespace steer.Models
{
    public class UpFile
    {
        [Key]
        [Display(Name="Auðkenni")]
        public int ID { get; set; }

        [Required]
        [Display(Name="Nafn")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "Hlaðið þann")]
        public DateTime Date { get; set; }

        [Required]
        [Display(Name="Tegund")]
        public string FileType { get; set; }

        [Required]
        [Display(Name = "Stærð (kB)")]
        public int Size { get; set; }

        public string Columns { get; set; }

        public virtual ICollection<Field> Fields { get; set; }

        public class UpFileDBContext : DbContext
        {
            public DbSet<UpFile> UpFiles { get; set; }
        }
    }

    public class Field
    {
        public int ID { get; set; }
        public string values { get; set; }
        [ForeignKey("UpFile")]
        public int UpFileID { get; set; }
        public virtual UpFile UpFile { get; set; }

        public class FieldDBContext : DbContext
        {
            public DbSet<Field> Fields { get; set; }
        }
    }
}

我怀疑问题是你在查询中使用复杂类型进行比较f.UpFile.Equals(upFile)

你为什么不尝试比较一个原始类型,例如upFile id?

我会尝试:

 var fields = from f in db2.Fields
                      where f.UpFile.ID == upFile.ID
                      select f;
 ViewBag.Fields = fields.ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM