When using LINQ and EF in a project, there was a problem with the exception shown in the topic, and the search for a lot of data found no solution, mainly because of the lack of knowledge of EF.
The situation is recorded as follows for future reference.
The query primarily designs two tables, which are associated by foreign keys:
An exception occurred while making the following query: Cannot cast type ' system.nullable ' 1 ' to type ' System.Object '. LINQ to entities only supports casting EDM primitives or enumeration types.
PublicActionResult getipsegments () {//list<ipsegment> ipsegments = (from S in devicedb.ipsegment select s). ToList (); varIpsegments = fromDinchDevicedb.devicecategory Join Sinchdevicedb.ipsegment on D.devicecategoryid equals S.devicecategoryid Select New{devicecategoryid1=S.devicecategoryid, Devicecategoryname1=D.devicecategoryname, Ipsegment="202.115.242."+ S.ip_head +"-202.115.242."+S.ip_end}; returnJson (ipsegments, jsonrequestbehavior.allowget); }
Later, the query was modified to be successful. The modified query looks like this:
Public actionresult getipsegments () { var from in devicedb.ipsegment Selectnew { S.devicecategoryid, s.devicecategory.devicecategoryname, s.ip_head, s.ip_end }; return Json (ipsegments, jsonrequestbehavior.allowget); }
The reasons for this are not yet understood, and then supplemented by understanding.
The
Cannot cast the type "System.Nullable ' 1" to type "System.Object". LINQ to entities only supports casting EDM primitives or enumeration types.