LINQ to Entities through Interface Property

坚强是说给别人听的谎言 提交于 2019-12-05 05:02:27

This is not supported. Your Linq-to-entities query can use only mapped properties of your entities. If you use interface properties EF doesn't know how to convert them to SQL because it is not able to analyze your code in property implementation.

Don't use interfaces for entities - EF doesn't support it at all. In your special case it will even not work with any other ORM because you are querying on properties which are unknown to mapping. This would require you to build your own Linq provider translating your query to query with real mapped properties.

The following exception occures during query execution based on a generic source and with interface member used in the where clause.

NotSupportedException: The mapping of interface member [InterfaceName].[MemberName] is not supported.

The exception occures only when the query should return multiple items and when I used == operator. I could not reproduce the error when executed the query with First, FirstOrDefault or Single, or when I used equals or other operator in the where clause.

Reference : Interface not supported

You can use Dynamic Query Library (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx).

        if (typeof (INamedEntity).IsAssignableFrom(typeof (T)))
        {
            q = q.Where("ID ==@0", id);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!