Search code examples
vb.netentity-framework-4.1ef-code-firstforeign-key-relationshipfluent-interface

Entity framework 4.1: a very special relationship


I have in my database a table (AccessControl) that describes a sort of users "access control list" for the informations contained in the table Customers and other tables.

Example: the entity CUSTOMER is marked by the EntityId #1. If a user belongs to the department #6, he can access the records of customer #16 and #31, but he can't for #14, that can viewed by user in department #3:

Table ACCESSCONTROL:

    EntityId    PrimaryKey  DepartmentId
    1       16      6
    1       31      6
    1       14      3

Here an example of the classes I am using in the domain:

Public Class Customer
   Public Property Id As Integer
   .......
   Public Overridable Property Acl As ICollection(Of AccessControl)
End Class

Public Class AccessControl
   Public Property EntityId As Integer
   Public Property PrimaryKey As Integer
   Public Property DepartmentId As Integer
End Class

How can I describe this relationship into the DbContext definition using a fluent Code First approach? Thank you in advance.


Solution

  • If I understand your problem correctly it is not possible to setup this relation in EF. There are many reasons why it will not work but the base is: unless you are able to set this relation in DB you cannot set it in EF as well. Your relation is data driven and EF has very limited support for data driven mapping - for example TPH inheritance which will not work in your scenario.