none
How do I manually load an EntityRef object? RRS feed

  • Question

  • I have a SQL-mapped class object that contains an EntityRef to another SQL-mapped class object. Both class objects are contained in the same database however the first class object will be loaded some time before the second (and so they each require their own instance of a datacontext.)

    private EntityRef<CATCUserUP> _oUPUser;

    [Association(Storage="_oUPUser",ThisKey="UP_EmpNo",OtherKey="Employee_Number")]
                public CATCUserUP UPUser { get { return _oUPUser.Entity; } }

    (note how the property is read-only)

    If the class object containing this code was loaded "some time ago", how do I get this object to load/instantiate itself after the fact? And how does it know what datacontext to use when it is told to load itself?

    Monday, September 21, 2009 9:54 PM

Answers

  • When you first access the UPUser property it will load the entity from the database automatically using the same data context that the "first class object" came from.

    You can't have references between entities that come from different data contexts.

    [)amien
    • Marked as answer by Zhipeng Lee Monday, September 28, 2009 1:26 AM
    Tuesday, September 22, 2009 3:08 AM
    Moderator

All replies

  • When you first access the UPUser property it will load the entity from the database automatically using the same data context that the "first class object" came from.

    You can't have references between entities that come from different data contexts.

    [)amien
    • Marked as answer by Zhipeng Lee Monday, September 28, 2009 1:26 AM
    Tuesday, September 22, 2009 3:08 AM
    Moderator
  • The two objects use the same data context type but I wanted to know if there is a way to load each object at different times using the same DataContext TYPE but different instances. For example:

    using (UPDataContext dc = new ...)
    {
     ClassA obj = (from a in dc.UP_Users where ... select a).SingleOrDefault();
    }

    // ... some time later ...
    using (UPDataContext dc2 = new ...)
    {
    // Here I want to load objA.UPuser but I don't know "how" per say since the only method I know of to load an EntityRef is by referencing it in a LINQ query.
    }

    Is my only choice to have any EntityRef's from ClassA loaded when ClassA is loaded (i.e. with a .LoadsWith()?)
    Tuesday, September 22, 2009 2:28 PM
  • No, entities can not have associations to other entities that came from a different instance of the data context. Think of it as a unit of work where all operations relating to that operation take place.

    You don't have to load entityrefs from ClassA when it is loaded - they can be loaded later automatically just by accessing the property.

    [)amien
    Tuesday, September 22, 2009 2:32 PM
    Moderator
  • No, entities can not have associations to other entities that came from a different instance of the data context. Think of it as a unit of work where all operations relating to that operation take place.

    You don't have to load entityrefs from ClassA when it is loaded - they can be loaded later automatically just by accessing the property.

    [)amien

    If a property/EntityRef can be loaded simply by accessing the property, what context does it use to load that property?

    using (TypedDC dc = new ..)
    {
    ClassA obj = (from a in dc.Users where ... select a).SingleOrDefault();
    }

    // Access a property from the EntityRef that supposedly did not load during the datacontext first established.
    MessageBox.Show(obj.UPUser.Employee_Number);

    That last line - what context does it use when the original context for the class object is disposed at the time of invocation?
    Tuesday, September 22, 2009 3:05 PM