0

I'm currently using Entity Framework 7.0.0-beta3 and am trying to create a many-to-many relationship between two tables. For that I'm using the syntax from The answer of this question, minus the ICollections:

public class Permissions
{
    public Guid PermissionId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}
public class relUserPermissions
{
    public string UserId { get; set; }
    public ApplicationUser User { get; set; }
    public Guid PermissionId { get; set; }
    public Permissions Permission { get; set; }
}

ApplicationUser is referring to the AspNetUsers Table. Using the kpm migration tool, this will create the relUserPermissions Table with foreign keys to both AspNetUsers and Permissions.

As I'm trying to access this via LINQ, I need to add the relation to the Database Context, using public DbSet<relUserPermissions> relUserPermission { get; set; }. But once I add that line, the foreign key to Permissions is no longer generated, the foreign key to AspNetUsers still is.

Community
  • 1
  • 1
hakai
  • 329
  • 3
  • 13

3 Answers3

0

For creating relationship you have to provide it on both the table,

try this:

public class Permissions
{
    public Guid PermissionId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public IList<relUserPermissions> relUserPermission {get; set;}
}
public class relUserPermissions
{
    public string UserId { get; set; }
    public ApplicationUser User { get; set; }
    public Guid PermissionId { get; set; }
    public Permissions Permission { get; set; }
}

add this property in Permissions class

public Ilist<relUserPermisions> relUserPermission {get; set;}
gaurav bhavsar
  • 2,033
  • 2
  • 22
  • 36
  • I already tried that, with ICollection instead of Ilist. Did not work. I found a workaround in the meantime though. – hakai May 06 '15 at 14:42
0

While it is a bit annoying, having to do that, for now I was able to solve the problem by renaming Permissions.PermissionId to Permissions.Id. Not a great solution and I have no clue why this affects anything (I added PermissionId as a key via Fluent API before), but it's working.

hakai
  • 329
  • 3
  • 13
0

Add Navigation property as shown bleow

public ApplicationUser User { get; set; }
public Guid PermissionId { get; set; }
public Permissions Permission { get; set; }
public virtual Permissions Permission { get; set; }
Praneeth
  • 21
  • 3