What Are the Downsides to Turning Off Proxycreationenabled for Ctp5 of Ef Code First

What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first

Dynamic proxies are used for change tracking and lazy loading. When WCF tries to serialize object, related context is usually closed and disposed but serialization of navigation properties will automatically trigger lazy loading (on closed context) => exception.

If you turn off lazy loading you will need to use eager loading for all navigation properties you want to use (Include on ObjectQuery). Tracking changes doesn't work over WCF it works only for modification of entity which is attached to ObjectContext.

Entity Framework disable loading virtual members

Your title is very misleading if you suddenly say

I just don't want those two last columns when binding my data to dataGridView

The reason why those two columns appear in your dataGridView is because you are binding your models directly.

Here are some alternatives:

1.Remove the columns after binding.

dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);

2.Create a different view model for binding

public class DataGridViewModel
{
public int ID { get; set; }

public string Name { get; set; }

public int Type_ID { get; set; }

public decimal Price { get; set; }

public string Descryption { get; set; }

public int Available_amount { get; set; }

public DataGridViewModel()
{
}
}

public void GetProducts()
{
using (var db = new SklepContext())
{
var data = db.Products.Select(r => new DataGridViewModel()
{
ID = r.ID,
Name = r.Name,
Type_ID = r.Type_ID,
Price = r.Price,
Descryption = r.Descryption,
Available_amount = r.Available_amount
}).ToList();
dataGridViewBrowse.DataSource = data;
}
}

Turning off proxies results in no associations

Since you've turned off proxies, you probably need to load the associations with Include().

Entity Framework v4 POCO templates: repository returns object of incorrect type

I can confirm that the solution is to set

context.ProxyCreationEnabled = false;

which disables creation of dynamic proxy typed objects and leaves us with simple POCOs, which is what we were after with EF POCO templates in the first place.

But you lose lazy loading of navigation properties and change tracking on entities. For the first, you either have to use context.LoadProperty() or the Include() method on your ObjectQuery object. For the second, I do not know the solution yet (actually it doesn't really make sense to have change tracking on POCOs).

Also here is a similar question I would like to point out
What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first

Entity Framework 6: Updating entity when tracking is disabled on context

Without ProxyCreationEnabled, EF will not eagerly load child entities for your object without explicitly "Including" them and change tracking will be disabled as well.

If you use AsNoTracking, you will have to set the entities states to Modified on the object context in order to persist changes. You can do that with code like this:

context.Entry(entity).State = EntityState.Modified;

Here are a couple of other SO questions that go deeper into your question.

What are the downsides to turning off ProxyCreationEnabled for CTP5 of EF code first

DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState.Modified

DataContractSerializer Error using Entity Framework 4.0 with WCF 4.0

This was a pain to figure out but it is because EntityFramework creates a 'proxy' of your class. The TestObject class I had was setup correctly, but it was creating a class called: TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE

To make the ChannelFactory + WCF + Entity Framework all work together, you must go into your Context constructor and add the following:

ContextOptions.ProxyCreationEnabled = false;

I hope this helps someone else.



Related Topics



Leave a reply



Submit