locked
relationship ,entityset,propery as foreign key. RRS feed

  • Question

  • Now I'm creating two classes as entity which have on-to-many relationship like customer and order in northwind.

     

    First I created classes by hand.I wrote entityset property and relationship attribute.

    BuI encountered that the property for foreign key of child entity was not set when inserting.

    My code to insert entity to database is like below.

     

     customer.Orders.Add(order);

     

    With above code,new records for parent and child entitry were inserted to database,but field for foreign key was still null.

     

    So,I tried to create entities by O/R designer.I created both entities and add relation between them.Then I generated c sharp code.

    But It didn't generate Orders property for Customer class and Customer property for Order class.And property for foreign key of child class was null also.

     

    I missed something to do?I have to write something by hand?

    Any information is helpful for me.

    Thanks.

     

     

    Monday, February 25, 2008 10:03 AM

Answers

  • I solved problem by my own.

     

    I recreated relationships between table,then I could generate valid source code for classes of entities.

    I don't know what was wrong,but now everything is working fine.

    Monday, February 25, 2008 4:47 PM

All replies


  • You could check to see if you have Foreign Key relationship declared in your database by running sp_help '[table name]'. Near the bottom you should see the FKs if there are any. I don't believe LINQ-to-SQL ORM designer can guess the relationship without the implicit FK definitions


    If you would to turn on SQL Profiler you would see this query sent by the designer:



    Code Snippet

    SELECT
    db_name() AS [Database_Name],
    SCHEMA_NAME(tbl.schema_id) AS [Table_Schema],
    tbl.name AS [Table_Name],
    cstr.name AS [Name],
    ki.name AS [ReferencedKey],
    rtbl.name AS [ReferencedTable],
    schema_name(rtbl.schema_id) AS [ReferencedTableSchema],
    cstr.update_referential_action AS [UpdateAction],
    cstr.delete_referential_action AS [DeleteAction]
    FROM
    sys.tables AS tbl
    INNER JOIN sys.foreign_keys AS cstr ON cstr.parent_object_id=tbl.object_id
    LEFT OUTER JOIN sys.indexes AS ki ON ki.index_id = cstr.key_index_id and ki.object_id = cstr.referenced_object_id
    INNER JOIN sys.tables rtbl ON rtbl.object_id = cstr.referenced_object_id
    WHERE
    (SCHEMA_NAME(tbl.schema_id)=N'dbo' and tbl.name=N'Products')
    ORDER BY
    [Database_Name] ASC,[Table_Schema] ASC,[Table_Name] ASC,[Name] ASC



    Petar.
    Monday, February 25, 2008 4:40 PM
  • I solved problem by my own.

     

    I recreated relationships between table,then I could generate valid source code for classes of entities.

    I don't know what was wrong,but now everything is working fine.

    Monday, February 25, 2008 4:47 PM
  • Thanks petar.

    I'll try your suggestion if I encounter same kind of problem again.

     

    Monday, February 25, 2008 4:55 PM