System.NotSupportedException: 'The specified type member 'StockRecord' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'
I have two SQL tables, the Shareholder table which is associated by a FK to the StockRecord table. My objective is to select items from the Shareholder table where the field PropertyPrime contains info in the "search" variable. The field PropertyPrime is
a column in the second table called Stockholder.
My Shareholder table has a model which includes -
public partial class Shareholder
{
public int ShareholderID { get; set; }
public int Certificate { get; set; } //FK to StockRecord Table PK
...Other columns....
public string Notes { get; set; }
public virtual string PropertyPrime { get; set; }
public virtual StockRecord StockRecord { get; set; }
}
The controller code is as follows and I receive the error where yellow highlighted -
System.NotSupportedException: 'The specified type member 'StockRecord' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'
I have two SQL tables, the Shareholder table which is associated by a FK to the StockRecord table. My objective is to select items from the Shareholder table where the field PropertyPrime contains info in the "search" variable. The field PropertyPrime is
a column in the second table called Stockholder.
My Shareholder table has a model which includes -
public partial class Shareholder
{
public int ShareholderID { get; set; }
public int Certificate { get; set; } //FK to StockRecord Table PK
...Other columns....
public string Notes { get; set; }
public virtual string PropertyPrime { get; set; }
public virtual StockRecord StockRecord { get; set; }
}
The controller code is as follows and I receive the error where yellow highlighted -
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
4 Points
8 Posts
Exeception User-Unhandled - Not Supported in LINQ to Entities.
Apr 05, 2020 08:50 PM|ikeni|LINK
Hi - I receive the following error -
System.NotSupportedException: 'The specified type member 'StockRecord' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.'
I have two SQL tables, the Shareholder table which is associated by a FK to the StockRecord table. My objective is to select items from the Shareholder table where the field PropertyPrime contains info in the "search" variable. The field PropertyPrime is a column in the second table called Stockholder.
My Shareholder table has a model which includes -
public partial class Shareholder
{
public int ShareholderID { get; set; }
public int Certificate { get; set; } //FK to StockRecord Table PK
...Other columns....
public string Notes { get; set; }
public virtual string PropertyPrime { get; set; }
public virtual StockRecord StockRecord { get; set; }
}
The controller code is as follows and I receive the error where yellow highlighted -
public ActionResult Index(string searchBy, string search, string includeItems, int? page)
{
var shareholders = db.Shareholders.Include(s => s.StockRecord.PropertyPrime);
if (search == null) { search = " "; }
if (searchBy == "PropertyPrime" && includeItems != "B")
{
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else if (searchBy == "PropertyPrime" && includeItems == "B")
{
return View(db.Shareholders.Where(s => s.StockRecord.PropertyPrime.Contains(search) || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else if (searchBy == "CertificateTitle" && includeItems != "B")
{
return View(db.Shareholders.Where(s => s.LastName.Contains(search) && s.Status == includeItems || search == null).ToList().ToPagedList(page ?? 1, 10));
}
else _ = (searchBy == "CertificateTitle" && includeItems == "B");
{
return View(db.Shareholders.Where(s => s.LastName.Contains(search) || search == null).ToList().ToPagedList(page ?? 1, 10));
}
//
}
Many Thanks, Ken
Contributor
4983 Points
4265 Posts
Re: Exeception User-Unhandled - Not Supported in LINQ to Entities.
Apr 05, 2020 09:54 PM|DA924|LINK
The message is clear. You cannot use StockRecord in the manner you're trying to use it in Linq-2-Entities. I don't even know what you're trying to do.
Participant
1320 Points
491 Posts
Re: Exeception User-Unhandled - Not Supported in LINQ to Entities.
Apr 06, 2020 01:56 AM|jiadongm|LINK
Hi ikeni,
You seem want to query through the properties of associated objects, if so, you should use .include() method first load the associated object.
For more details, you can refer to
https://docs.microsoft.com/en-us/ef/ef6/querying/related-data
https://www.entityframeworktutorial.net/eager-loading-in-entity-framework.aspx
Best Regards,
Jiadong Meng
Member
4 Points
8 Posts
Re: Exeception User-Unhandled - Not Supported in LINQ to Entities.
Apr 30, 2020 04:09 PM|ikeni|LINK
Thank You!