Linq-to-Sql: adding a condition (where) to a column that is not in the metamodel
I'm building ASP.Net MVC2 application on top of a legacy database. Every table in the database includes an id that defines to what organization it belongs to. Like:
| id | name | org_id |
| 1 | John | 4 |
| 2 | Joe | 4 |
| 3 | Anne | 2 |
Now, user "John" should not see Anne from another organization e.g. get that row from the database, ever. My model would be something like this
[Table(Name="person")]
public class Person
[Column] public int id { get; set; }
[Column] public string name { get; set; }
org_id is not included to the model because it really isn't domain model in that sense. It is there just to filter results (and eventually cascade organization deletes in the database).
I have a repository class that returns IQueryable<Person>
using DataContext.GetTable<Person>
and thus enables lazy loading.
How can I insert to that IQueryable
or to the DataContext
custom Where-clause th开发者_StackOverflow中文版at would require the org_id
to be 4.
Writing
return (new DataContext(connectionString)).GetTable<CardBase>().Where(x => x.NOORGIDHERE...
results only filtering by proerties from the Person-class.
Either use a stored procedure, or map that column as read-only (and perhaps internal, so you can create a wrapper for it, without exposing the property).
精彩评论