none
No child to parent one-to-one auto-generated association? RRS feed

  • Question

  • I'm creating a simple SQL Server database with just two tables: Customers and Invoices.  One customer can have multiple invoices.  I've created a foreign key in Invoices linking the Invoice table with the Customer table.  When I create the LINQ to SQL dbml in Visual Studio 2010 Pro by dragging and dropping the two tables onto the dbml design surface, it auto-generates a one-to-many association from the Customer table to the Invoice table.  Is there a way to configure the SQL Server tables so that Visual Studio also auto-generates the reverse one-to-one association linking an Invoice back to a Customer so that I don't have to create this manually?
    Saturday, June 4, 2011 3:01 AM

Answers

All replies

  • Hello,

    There are three ways to keep the model in sync.

    1. Delete the modified tables from the designer, and drag them back onto the designer surface from the Database Explorer.

                    a. Refresh the database schema in the Database Explorer.

                    b. Save the designer after deleting the tables.

                    c. Save again after dragging the tables back.

    2. Use SQLMetal to regenerate the schema from your database. You can check this article for more information. http://www.codeproject.com/KB/database/SqlMetalPlus.aspx?display=Mobile

    3. Make changes directly in the properties pane of the DBML.

    Hope this helps. If I misunderstood, please feel free to let me know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 7, 2011 6:32 AM
  • Thanks for the suggestions Larcolais.  SQLMetal looks like a useful addon.  However, it seems like Visual Studio should create associations in both directions when autogenerating the DBML.  A One-to-Many (parent-to-child) relationship always has a reverse One-to-One (child-to-parent) relationship.  That would save having to create the One-to-One associations manually.
    Tuesday, June 7, 2011 6:50 AM
  • Hello again,

    From your latest reply, it looks that you should use Entity Framework to achieve this. I think this is the great pratice for you. Please check it. http://blogs.msdn.com/b/msdnforum/archive/2010/05/07/foreign-key-association-in-entity-framework-4.aspx

    Hope this helps. If you have any question, welcome to post back.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 8, 2011 8:28 AM