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

    Question

  • I have three tables, Employee, Permissions and PermissionsEmployee

    "Employee"
    ID (string)
    Name (string)

    "Permissions"
    ID (Int16)
    Description (string)

    "PermissionsEmployee"
    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.AddToPermissionsEmployee(newPermEmployee);

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

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

     proxy.SaveChanges();

     

    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