Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Very good question, and Yes, there is a perfect way to accomplish this in EF4: <br><br> <strong><em>Custom properties</em></strong> are a way to provide computed properties to entities. The good news is that Custom properties don’t necessarily need to be calculated from other existing properties on the very same entity, by the code we are about to see, they can computed from just about anything we like! <br><br> Here are the steps: <br> First create a partial class and define a custom property on it (For simplicity, I assumed <em>User_User</em> table has been mapped to User class and <em>Util_Login</em> to Util) <br></p> <pre><code>public partial class User { public DateTime LastLoginDate { get; set; } } </code></pre> <p>So, as you can see here, rather than creating a LastLoginDate property in the model, which would be required to map back to the data store, we have created the property in the partial class and then we have the option to populate it during <strong><em>object materialization</em></strong> or <strong><em>on demand</em></strong> if you don’t believe that every entity object will need to provide that information. <br><br> In your case precalculating the <em>LastLoginDate</em> custom property for every <em>User</em> being materialized is useful since I think this value will be accessed for all (or at least most) of the entities being materialized. Otherwise, you should consider calculating the property only as needed and not during object materialization. <br><br> For that, we are going to leverage <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectmaterialized.aspx" rel="nofollow noreferrer"><strong><em>ObjectContext.ObjectMaterialized Event</em></strong></a> which is raised anytime data is returned from a query since the ObjectContext is creating the entity objects from that data. ObjectMaterialized event is an Entity Framework 4 thing. So all we need to do is to create an event handler and subscribe it to the ObjectMaterialized Event. <br><br> The best place to put this code (subscribing to the event) is inside the <strong><em>OnContextCreated Method</em></strong>. This method is called by the context object’s constructor and the constructor overloads which is a partial method with no implementation, merely a method signature created by EF code generator. <br><br> Ok, now you need to create a partial class for your ObjectContext. (I assume the name is <em>UsersAndLoginsEntities</em>) and subscribe the event handler (I named it <i>Context_ObjectMaterialized</i>) to <em>ObjectMaterialized Event</em>. <br></p> <pre><code>public partial class UsersAndLoginsEntities { partial void OnContextCreated() { this.ObjectMaterialized += Context_ObjectMaterialized; } } </code></pre> <p>The last step (the real work) would be to implement this handler to actually populate the Custom Property for us, which in this case is very easy:</p> <pre><code>void Context_ObjectMaterialized(object sender, ObjectMaterializedEventArgs args) { if (args.Entity is User) { User user = (User)args.Entity; user.LastLoginDate = this.Utils .Where(u => u.UserID == user.UserID) .Max(u => u.LoginDate); } } </code></pre> <p><br> Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload