Results 1 to 3 of 3

Thread: Entity Framework and relationships in Vb

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    420

    Entity Framework and relationships in Vb

    I have recently being following the various tutorials on creating models in the entity framework and reading them with vb code.

    I have managed to create a model and add the relationships between the tables in the model.

    These can be one to one, one to many etc.

    What I can't understand is when the relationship is set between the tables, what are keys used to link the two tables.

    The properties for the link simply shows the other table and not the two fields.

    Whilst I many not be understanding it correctly, I am expecting the value in one table to be looked up (as the key) in the next table ?
    Last edited by Signalman; May 23rd, 2013 at 02:55 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Entity Framework and relationships in Vb

    I'm not 100% sure what you're asking but I'll post a couple of diagrams that hopefully show how relationships work in the database and in the EF model. Here's a screenshot of a diagram of a SQL Server database with 7 tables. I've lined up the table to show exactly which columns are related to which.
    Name:  EFRelationTestDatabase.PNG
Views: 841
Size:  36.3 KB
    The first two tables are engaged in a 1:1 relationship, with the primary key of the child table also being a foreign key from the parent table. The next two tables are engaged in a 1:many relationship, with the child table having its own PK and another column that is a FK from the parent table. The last three tables are engaged in a many:many relationship created from two 1:many relationships, with the join table existing solely to contain a FK from each of the sibling tables in the relationship. Here's how that looks in the EF designer:
    Name:  EFRelationTestModel.PNG
Views: 413
Size:  22.6 KB
    That EF model is in VS 2012. Notice that, when selecting the relationship between Parent1ToM and Child1ToM, it highlights all the properties involved.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    420

    Re: Entity Framework and relationships in Vb

    Thanks for spending the time to create the example, this is most helpful, however am correct in assuming that when creating the relationship / association in the entity model, it's always best to create foreign keys.

    Also, when the relationship / association is created, whilst I can see that it is created between the tables, in your example, there are identical field names in both files. As such I assume that the link / relationship between the two files in based on values in these fields 'matching'

    However looking at the model, and the link property's, I can't see where the field names that form the relationship are specified.

    The example i am looking at, does not seem to have the same field names in both tables, and when I add a advocation, it crates a foreign key, and adds an exa field to one for the tables.

    However again how do I see what the fields are that the relationship / association is between ?

    Also when adding records to the file with foreign keys, do I have to manually update the addition field or is it automatically taken care of ?

    Many thanks for your help
    Last edited by Signalman; May 26th, 2013 at 03:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width