none
Creating foreign keys in POCOs with bridge table (EF4.1 Update 1) RRS feed

  • General discussion

  • Entities:

    Address
      - [Key] public long AddressID { get; set; }
      - public virtual ICollection<Address_Contacts> Contacts { get; set; }
      ...

    Address_Contacts
      - [Key] public long AddressID { get; set; }
      - [Key] public long ContactID { get; set; }
      - public in ContactToAddressRelationshipID { get; set; }     //this property causes this bridge table to be necessary
      - public virtual Contact Contact {get; set; }
      - public virtual ContactToAddressRelationship ContactToAddressRelationship {get; set; }
      - public virtual Address Address { get; set; }

    Contact
      - [Key] public long ContactID { get; set; }
      - public virtual ICollection<Address_Contacts> Addresses { get; set; }
      ...

    So when I try to set up the foreign key associations in the POCOs, I keep getting the following vague silverlight error at some point during the call to my domain service:

    [Window Title]
    Visual Studio Just-In-Time Debugger
    
    [Main Instruction]
    An unhandled exception ('Unhandled Error in Silverlight Application 
    Code: 4004    
    Category: ManagedRuntimeError       
    Message: System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query 'GetLocationsByName'. The remote server returned an error: NotFound. ---> System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
       at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
       at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
       at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
       --- End of inner exception stack trace ---
       at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
       at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
       at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
       --- End of inner exception stack trace ---
       at System.ServiceModel.DomainServices.Client.WebDomainClient`1.EndQueryCore(IAsyncResult asyncResult)
       at System.ServiceModel.DomainServices.Client.DomainClient.EndQuery(IAsyncResult asyncResult)
       at System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult)
       --- End of inner exception stack trace ---
       at System.ServiceModel.DomainServices.Client.OperationBase.Complete(Exception error)
       at System.ServiceModel.DomainServices.Client.LoadOperation.Complete(Exception error)
       at System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult)
       at System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass1b.<Load>b__17(Object )     
    ') occurred in iexplore.exe [8264].
    
    The Just-In-Time debugger was launched without necessary security permissions. To debug this process, the Just-In-Time debugger must be run as an Administrator. Would you like to debug this process?
    
    [V] View process details  [Yes, debug iexplore.exe] [No, cancel debugging]
    
    [Expanded Information]
    Process Name: C:\Program Files (x86)\Internet Explorer\iexplore.exe


    Additionally, I get the following vague exception information in the output window:

    A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
    A first chance exception of type 'System.Net.WebException' occurred in System.Windows.dll
    A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
    A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
    A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
    A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
    A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
    A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.DomainServices.Client.Web
    A first chance exception of type 'System.ServiceModel.DomainServices.Client.DomainOperationException' occurred in System.ServiceModel.DomainServices.Client
    A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll


    The way that I'm currently setting up the foreign key associations is as follows:

    Address

    [Include]
    [Association("Address_Contacts_Association", "AddressID", "AddressId")]
    public virtual ICollection<Address_Contacts> Contacts { get; set; }


    Address_Contact

    [Key()]
    public long AddressID { get; set; }

    [Key()]
    public long ContactID { get; set; }

    // seems strange that I have to do this...
    public long AddressId { get; set; }
    public long ContactId { get; set; }


    Contact

    [Include]
    [Association("Contact_Addresses_Association", "ContactID", "ContactId")]
    public virtual ICollection<Address_Contacts> Addresses { get; set; }

    NOTE: After I added the associations, I ran a test before modifying my domain service just to make sure that everything was cool but got the exceptions before I got to the point of modifying the domain service method.  I can actually debug my domain service method and watch it complete before getting the silverlight exception.

    Does anyone see what I'm doing wrong?


    • Edited by IndyLB Wednesday, May 16, 2012 4:42 PM
    Wednesday, May 16, 2012 4:38 PM

All replies

  • Hi IndyLB,

    Welcome to MSDN Forum.

    To create a many to many relationship in Code First, you needn't to create a bridge table. For example.

    Class TestA
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<TestB> testBs { get; set; }
    }
    
    Class TestB
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<TestA> testAs { get; set; }
    }

    Now, TestA and TestB are many to many, you needn't to create another table to connect them.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 17, 2012 6:30 AM
    Moderator
  • Thanks for the reply Allen.  But that isn't going to work in this scenario.  I have other tables where in the many-to-many relationship is set up exactly as you describe and everything works fine.  The difference in this scenario is the "ContactToAddressRelationship" property in the bridge table.  

    That property is a foreign key to another table that describes the relationship between the address and the contact.  For example, one of the contacts for a specific address could be the homeowner, landlord, tenant, etc. (and there could be multiple contacts for an address).  So because I need to describe the relationship in the bridge table, EF included the bridge table in the conceptual model when it was created from the database (didn't use code-first).  I hope that explains the situation a little better.  Below is the screen shot of the model in case that helps:



    • Edited by IndyLB Thursday, May 17, 2012 12:24 PM
    Thursday, May 17, 2012 12:23 PM