0

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!

tereško
  • 58,060
  • 25
  • 98
  • 150
super
  • 2,288
  • 2
  • 21
  • 23

3 Answers3

3

The reason you are getting this because your LINQ query is getting converted to SQL query and since you have specified Status property with [NotMapped] attribute, It can't find the column in the table schema and hence generating the error

Habib
  • 219,104
  • 29
  • 407
  • 436
0

You should map your SmsContentStatus entity or use some statusId which also should be mapped.

Sanja Melnichuk
  • 3,465
  • 3
  • 25
  • 46
0

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
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Get an error like this : The model item passed into the dictionary is of type 'Sample.Models.SmsContent', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Sample.Models.SmsContent]'. – super Oct 08 '12 at 10:35