Why newly created entity object with navigation property is automaticly added to ObjectContext?
Posted
by Levelbit
on Stack Overflow
See other posts from Stack Overflow
or by Levelbit
Published on 2010-04-13T08:16:57Z
Indexed on
2010/04/14
9:03 UTC
Read the original article
Hit count: 168
entity-framework
I have to entities: Company and Location (one to many). When I create new Location entity object and assign navigation property(Company) with the navigation property of already existing Location object (Location _new = new Location(); _new.Company = _old.Company
). It seems that at that point newly created object is added to Object Context automatically, because when I call SaveChanges method that object is insert to database although I didn't call ObjectContext.AddObject(_new). I'm new in EF so there is probably reason why I have result like this? Is there need to assign also CompanyReference filed too and how to do it?
IDaoFactory daoFactory = new DaoFactory();
ILocationDao locaitonDao = daoFactory.GetLocationDao();
IEnumerable<Location> locations = locaitonDao.GetLocations();
Location _old = locations.First();
Location _new = new Location();
_new.LocationName = _old.LocationName;
_new.Company = _old.Company;// 1
_new.Address = _old.Address;
//...
ContactEntities.SaveChanges();//2If I execute line (1) instantly _new object is added to object context and I can see additional datarow in my datagrid after line (2) is executed.
© Stack Overflow or respective owner