LINQ to Entities table with foreign keys that are also primary keys


  • I have three tables, Employee, Permissions and PermissionsEmployee

    ID (string)
    Name (string)

    ID (Int16)
    Description (string)

    EmployeeID (string) [part of composite primary key and foreign key to ID on Employee table]
    PermissionID (Int16)[part of composite primary key and foreign key to ID on Permissions table]

    Using LINQ to Entities with Framework 3.5, I am trying to add a record to PermissionsEmployee. Both EmployeeID and Permission ID already exist in perspective tables

    TTDataLayer.PermissionsEmployee newPermEmployee = new TTDataLayer.PermissionsEmployee();

    var employeeAddResult = ttProxy.Employee.Where(e => e.UserID == (strEmpID)).FirstOrDefault();

     var permAddResult = ttProxy.Permissions.Where(p => p.ID == (intPermID)).FirstOrDefault();

    newPermEmployee.EmployeeID = strEmpID;

    newPermEmployee.PermissionID = intPermID


    proxy.SetLink(newPermEmployee, "Employee", employeeAddResult);

    proxy.SetLink(newPermEmployee,"Permissions", permAddResult);



    I have no problem saving tables with foreign keys when these are not also primary keys. I just call the appropriate SetLink and all is well.
    This currently gives me a generic error "An error occurred while processing this request." . Anybody have an idea on how I need to go about adding a record here?


    Wednesday, March 10, 2010 9:48 PM

All replies