Entity Framework 6 Recipes 2nd Edition (13-10)---Explicitly create proxy

Source: Internet
Author: User

Problem

You have a Poco entity that creates proxies dynamically when executing a query, and now you don't want EF to delay the cost of creating proxies.

Solution Solutions

Suppose you have a figure13-15 model.

Figure 1 3 -15. A model for CDs and music titles

The corresponding Poco class, such as code listing 13-27. Note: Each property uses the virtual keyword and each reference property as the Icoolection class, and EF dynamically creates a tracking agent for the entity (for more information, see recipe 13-5).

Listing 13-27. The POCO Classes along with our Object Context

public partial class CD

{

Public CD ()

{

This. Tracks = new hashset<track> ();

}

public int Cdid {get; set;}

public string Title {get; set;}

Public virtual icollection<track> Tracks {get; set;}

}

public partial class track

{

public string Title {get; set;}

public string Artist {get; set;}

public int Cdid {get; set;}

}

In order for EF to create a proxy before it is needed (before an entity is loaded), we need to use the ObjectContext createproxytypes () method, such as code listing 13-28.

Listing 13-28. Generating the tracking proxies before loading the entities

using (var context = new Efrecipesentities ())

{

In order to trigger proxy creation, we need to drill down to the underlying ObjectContext because DbContext does not expose the Createproxytypes () method

var objectContext = ((iobjectcontextadapter) context). ObjectContext;

Objectcontext.createproxytypes (new type[] {typeof (CD), typeof (Track)});

var proxytypes = Objectcontext.getknownproxytypes ();

Console.WriteLine ("{0} proxies generated!", Objectcontext.getknownproxytypes (). Count ());

var CDs = context. Cds.include ("Tracks");

foreach (var cd in CDs)

{

Console.WriteLine ("Album: {0}", CD. Title);

foreach (var track in CD. Tracks)

{

Console.WriteLine ("\t{0} by {1}", track. Title, track. Artist);

}

}

}

The output results are as follows:

2 proxies generated!

Album:abbey Road

Come Together by the Beatles

Album:cowboy Town

Cowgirls Don ' t Cry by Brooks & Dunn

Album:long Black Train

In My Dreams by Josh Turner

Jacksonville by Josh Turner

How it works

Dynamic proxies are created only when they are needed, that is, when the load occurs in the first query, this deferred creation works well in most cases. However, you can create an agent before the entity is first loaded, by calling the Createproxytypes () method.

The Createproxytypes () method takes an array of types (entity type) as a parameter, it creates the corresponding entity proxy, once created, the life cycle of the agent is the same as the life cycle of the application domain, note that it is bound to the application domain, not the context object, we can release the context object, It then re-creates a context object, but the proxy is not freed and you can retrieve it from the application domain using the static method of the context object Getknowproxytypes ().

I note: End of performance chapter

Entity Framework 6 Recipes 2nd Edition (13-10)---Explicitly create proxy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.