Reputation: 29233
I have a model that I get from a POST request. Since my view defines its POCO type, the object created from the submitted data is also a POCO. Being a POCO, it doesn't have various virtual properties overridden. Therefore, those virtual properties return null. This, in turn, means that I have to make separate queries based on the foreign keys to navigate through its properties (if I want to do anything more complex than just saving it).
Can I, given the POCO of my model, get the proxy that has all the overridden functionality?
(I had assumed that this is what db.Entry().Entity
was for, but it still returns me the POCO object, not the proxy. I'm inspecting the runtime type of the object by mouse-over during breakpoint pauses.)
Upvotes: 9
Views: 2066
Reputation: 844
If you want to do this via the MVC controller, you might use something like this as an action:
[HttpPost]
public ActionResult Update(int? id, FormCollection form)
{
// assumes repository will handle
// retrieving the entity and
// including and navigational properties
var entity = repository.Get(id);
if (entity == null)
{
throw new InvalidOperationException(string.Format("Not found: {0}", id));
}
if (TryUpdateModel(entity))
{
try
{
//
// do other stuff, additional validation, etc
repository.Update(entity);
}
catch (Exception ex)
{
//
// exception cleansing/handling
// additional model errors
return View(entity);
}
return View("Success", entity);
}
return View(entity);
}
Upvotes: 0
Reputation: 14578
Something along the lines of this code will do what you need. I've used automapper to copy values from the passed in entity to the proxied version.
The code checks whether the passed in entity is a proxy or not and handles it accordingly.
public class Repository<T> where T : class
{
private readonly Context context;
private bool mapCreated = false;
public Repository(Context context)
{
this.context = context;
}
protected virtual T InsertOrUpdate(T e, int id)
{
T instance = context.Set<T>().Create();
if (e.GetType().Equals(instance.GetType()))
instance = e;
else
{
if (!mapCreated)
{
Mapper.CreateMap(e.GetType(), instance.GetType());
mapCreated = true;
}
instance = Mapper.Map(e, instance);
}
if (id == default(int))
context.Set<T>().Add(instance);
else
context.Entry<T>(instance).State = EntityState.Modified;
return instance;
}
}
UPDATE version as described by @Colin in the comments that does not need automapper
public class Repository<T> where T : class
{
private readonly Context context;
public Repository(Context context)
{
this.context = context;
}
protected virtual T InsertOrUpdate(T e, int id)
{
T instance = context.Set<T>().Create();
if (e.GetType().Equals(instance.GetType()))
{
instance = e;
}
else
{
DbEntityEntry<T> entry = context.Entry(instance);
entry.CurrentValues.SetValues(e);
}
context.Entry<T>(instance).State =
id == default(int)
? EntityState.Added
: EntityState.Modified;
return instance;
}
}
Upvotes: 7
Reputation: 12879
db.Entry().Entity will always return you a POCO, and will not return the proxy object that handles the implementation of virtual navigation properties:
var o = db.Entry(myPoco).Entity; // always returns a POCO
You will normally get a proxy object instead of a POCO when calling Find()
or Where()
against the database context. However, within the context in which an object is first added to the database, these methods will (unexpectedly?) return the POCO instead of the proxy object. You actually have to leave the context and open a new one to get the proxy:
// create a new POCO object, and connect to it to another object already in the DB
MyPoco myPoco = new MyPoco();
myPoco.MyOtherPocoId = myPoco2.MyOtherPocoId; // make reference to existing object
using (var db = new MyContext())
{
// Add myPoco to database.
db.MyPocos.Add(myPoco);
db.SaveChanges();
// One would think you get a proxy object here, but you don't: just a POCO
var test10 = db.MyPocos.Find(myPoco.Id); // returns a MyPoco
var test11 = db.MyPocos.Where(x => x.Id == myPoco.Id).First(); // returns a MyPoco
var test12 = db.Entry(myPoco).Entity; // returns a MyPoco
// ...so, you can't access the referenced properties through virtual navigation properties:
MyOtherPoco otherPoco1 = myPoco.Poco2; // returns NULL
}
// leave the context and build a new one
using (var db = new MyContext())
{
// Now, the same Find() and Where() methods return a proxy object
var test20 = db.MyPocos.Find(myPoco.Id); // returns a proxy object
var test21 = db.MyPocos.Where(x => x.Id == myPoco.Id).First(); // returns a proxy object
// ...which means the virtual properties can be accessed as expected:
MyOtherPoco otherPoco = myPoco.Poco2; // works as expected
// Note that db.Entry().Entity still returns a POCO:
var test22 = db.Entry(myPoco).Entity; // returns a MyPoco
}
There may be some magic incantation to make the context in which the object is added give you back a proxy object, but I haven't come across it.
Upvotes: 1