简体   繁体   中英

Specified type member"Status" not supported in LINQ to Entities

I have a class called PanContent

public class PanContent
    {        
        public int Id { get; set; }           
        public string Description { get; set; }
        public string Content { get; set; }            

        public PanContentStatus Status { get; set; }
        public ActivityId ActivityId { get; set; }       
    }

And I have an enum type PanContentStatus

public enum PanContentStatus
    {
        Active = 0,
        Partial,
        Inactive,
        Deleted
    }

I'm trying to use this in my Controller

public ActionResult Index()
    {
        var db = new TlDbContext();
        var status = PanContentStatus.Partial;

        var content = db.PanContents.Where(p => p.Status == status).FirstOrDefault();
        if (content != null)
        {
            return View(content);
        }
        else
        {
            return View();
        }
    }

and then use it in my view

@model IEnumerable<Sample.Models.PanContent>
<script type="text/javascript">
    var model = @Html.Raw(Json.Encode(Model));
    for (var m in model) {
                console.log(m.Content);
            }
</script>

but I'm getting the error "The specified type member 'Status' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."

Any help gratefully received!

你得到这个的原因是因为你的 LINQ 查询被转换为 SQL 查询,并且因为你已经指定了具有[NotMapped]属性的Status属性,它在表模式中找不到列,因此产生错误

您应该映射您的 SmsContentStatus 实体或使用一些也应该映射的 statusId。

If you're not using EF 5.0 (prior versions can't use directly enum)

change your model

add a new property

public int StatusValue { get; set; }

then change the other to

[NotMapped]
public SmsContentStatus Status 
{ 
   get {return (SmsContentStatus)StatusValue;}
   set {StatusValue = Convert.ToInt32(value); }
}

then in all linq to entities queries, use the int property ( StatusValue )

var status = SmsContentStatus.Partial;
var content = db.SmsContents.Where(p => p.StatusValue == (int)status).FirstOrDefault();
//or rather db.SmsContents.FirstOrDefault(p => p.StatusValue == (int)status);
//but that's a detail.

EDIT

if your view must represent a list, change your query to

var content = db.SmsContents.Where(p => p.StatusValue == (int)status);

if it must represent a single object, change your view's model to

@model Sample.Models.SmsContent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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