locked
Trying to share objects across models with the entity framework RRS feed

  • Question

  • Currently we are creating a separate datamodel for each page within an application.  Each model has 10 or 15 tables.

    We have a login page that validates the user using one model.

    Is there a simple way to make the user available in the other models?

    Relevant bits

    When the user logins in

    Imports LoginModel
    Private loginContext As LoginEntities   (contains USER and other tables)
    Public globalUserLoggedin as USER

        Private Shared Sub getUserComplete(ByVal result As IAsyncResult)
                Dim queryResult = CType(result.AsyncState, DataServiceQuery(Of USER))
                Dim results = queryResult.EndExecute(result)
                globalEmployeeLoggedIn = results(0)


    When an app page starts after the user logs in

    Imports AppModel
    Private appContext As appEntities    (contains USER and other tables)

    Public Sub New()

            InitializeComponent()
            appContext = New appEntities(New Uri("WebDataServices/wds_AppEntities.svc", UriKind.Relative))
            appContext.AttachTo("USER ", globalUserLoggedin)

    I’ve simplified this code but the real code compiles and runs (without complaining)

    Say the appContext contains a “CUSTOMER” and I want to say

    appContext.SetLink(oNewCustomer,”CREATEDBY”, globalUserLoggedin)

    With the above I get
    “The sourceProperty is not a reference or collection of the target's object type. Parameter name: target”

    Thanks for any help.

    • Moved by Diego B Vega [MSFT] Monday, April 27, 2009 8:45 AM not the right forum (From:ADO.NET Entity Framework and LINQ to Entities)
    Friday, April 24, 2009 1:32 PM

All replies

  • Hello,

    It appears that this question is about ADO.NET Data Services. I will move it for you to a more specific forum. Hopefully you will get the answer there soon.

    Thanks,
    Diego
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, April 27, 2009 8:44 AM
  • Hi,

    When you describe having multiple models, are you referring to there being more than one DataServiceContext instance? If so, do they all have the same service root?

    I'm just trying to get a clearer idea of what you're attempting, let me know if the following is accurate:

    1) Create a context for your service
    2) Retrieve a User entity instance and save it somewhere
    3) Create a new context, and add a new Customer entity
    4) Set the 'CreatedBy' reference property on the new customer to be the user we saved earlier

    Is that accurate?

    Can you share the metadata for the two entity types, or your entire service?

    Also, do you have the full stack trace for the error you are seeing?

    If you're uncomfortable posting this information, please email it to me at 'mmeehan' at microsoft.
    Matt Meehan, ADO.NET Data Services (Astoria)
    Monday, April 27, 2009 4:18 PM
    Moderator
  • Not sure if I'm understanding your problem, but maybe you could serialize the object and deserialize it as the correct type from the correct model.  I think it may be more manageable to use one model or keep the entities that need to work with e/o in the same model though. Hope that helps! -jogi
    Monday, April 27, 2009 11:21 PM
  • Hi Matt

    sorry for the delay in responding I lost track of this forum. 

    This is not quite accurate

    1) Create a context for your service
    2) Retrieve a User entity instance and save it somewhere
    3) Create a new context, and add a new Customer entity
    4) Set the 'CreatedBy' reference property on the new customer to be the user we saved earlier

    the changes are
    1) Create a context for your service with MODEL1
    2) Retrieve a User entity instance and save it somewhere
    3) Create a new context with MODEL2, and add a new Customer entity
    4) Set the 'CreatedBy' reference property on the new customer to be the user we saved earlier


    The error is
    "The sourceProperty is not a reference or collection of the target's object type. Parameter name: target”

    My terminology is weak so Model equates to an EDMX file with an associated service.  I have multiple EDMX files because the models are large and trying to work with all the tables in one is difficult.

    Tuesday, July 28, 2009 2:44 PM
  • Hi,
     It appears that the oNewCustomer object does not have a "CREATEDBY" navigation property.
     You can quickly verify this by looking at the type definition in the Service reference generated classes.
     In general, there is nothing tying down the entities to the DataServiceContext which was used to download them/

    This line :
     appContext.SetLink(oNewCustomer,”CREATEDBY”, globalUserLoggedin)

    should work, if the type of oNewCustomer has a CREATEDBY property of type User.
    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Monday, August 3, 2009 6:55 AM
    Moderator