Suppose I have two table tab1, tab2. EF will create a edmx file and there are two entities created.
Then I want to add a computer member to tab1, which get some result from tab2, such as count, sum. What I used is partial class:
public partial class tab1{
public int Count{
get{
int cnt;
//...
return cnt;
}
}
public int Total{
get{
int total;
//the matching sql like select sum(column) from tab2
return total;
}
}
}
I want to cnt is counts in tab2(so the SQL should be select count(1) from tab2). Or total which is calculated from another table.
But how to implement it? there is no something like datacontext yet.
Yes there is – use the ObjectContext ! That’s your “entry point into all things for Entity Framework.
You named it when you created the EDMX – it’s the same name that’s used for your Entity Framework connection string. It’s a class in your “mymodel.designer.cs” code-behind file for the EDMX model.
If your “tab1” and “tab2” entities are linked in a 1:n fashion (one “tab1” can have multiple “tab2” entries), you’ll find a member of “tab1” of type “EntityCollection” – let’s say this is called “tab2Entities”.
Now if you want to calculate the number of “tab2” entries for a given “tab1” object, just use this code:
That’s all there is! 🙂
Marc