开发者

Partial Class Properties not accessable through LINQ query bound to GridView

开发者 https://www.devze.com 2023-01-13 10:53 出处:网络
I have ad开发者_如何转开发ded a partial class to one generated by the Entity Framework. I wanted to add a calculated field that I could bind to my GridView. However, when I try to access the new prope

I have ad开发者_如何转开发ded a partial class to one generated by the Entity Framework. I wanted to add a calculated field that I could bind to my GridView. However, when I try to access the new property, I get an error message that this is a limitation to Entity Framework.

Are there any work arounds to accomplish this?


Without seeing your code it will be impossible to correctly diagnose your problem. However, I have a hunch...

Creating a partial class and adding a computed property to it is not a problem at all. Binding that same property to a GridView is also not a problem, because the GridView doesn't care as long as the property is public and accessing it doesn't itself throw an exception.

However... you might be trying to use that property in a query. Entity framework and every other ORM would puke on this one because there is no translation of that property into the underlying data store (probably SQL Server). So when EF tries to construct the SQL necessary to query the database it freaks out and says "I have no idea how to turn this property into valid SQL"

Make sure you aren't using that property in your queries and you should be fine.

UPDATE:

If you want to actually be able to use a computed column in a result set then you have two basic options:

1.) Create a view in the database with the computed column and bind your entity to that instead of the original underlying table. Your property will now be legitimately tied to something that can be queried against.

2.) Make sure you execute your query and bring back a full (unfiltered) result set back first, and then use regular LINQ to Object to get what you want. Something like this:

var query = from o in Context.Orders
            where o.Price > 100
            select o;

var orders = from order in query.ToList() // Force query to execute
             where order.ComputedColumn == 42
             select order;

Clearly this is less efficient than option one, but may not be horrible depending on the original query.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号