简体   繁体   中英

Composite key Entity Framework 7 "The Property Cannot Be added to Entity Type

I'm trying to make a enitity that manages membership of a user in a organization with a role. I want to restrict a user to have only one membership in an organization. I'm doing this by creating a composite key. However i get the error when i try to create the initial migrations:

InvalidOperationException: The property 'User' cannot be added to the entity type 'OrganizationLogin' because a navigation property with the same name already exists on entity type 'OrganizationLogin'.

The entity for membership

public class OrganizationLogin
{
    public int OrganizationLoginId { get; set; }
    public OrganizationRole Role { get; set; }
    public Organization Organization { get; set; }
    public OmegaUser User { get; set; } 

}

My DBContext where I try to define the composite key:

public class OmegaContext : IdentityDbContext<OmegaUser,OmegaRole,int>
{
    public DbSet<Log> Logs { get; set; }
    public DbSet<Organization> Organizations { get; set; }
    public DbSet<OrganizationLogin> OrganizationLogins { get; set; }
    public DbSet<OrganizationRole> OrganizationRoles { get; set; }

    public OmegaContext()
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<OrganizationLogin>(orgLogin =>
        {
            orgLogin.HasAlternateKey(o => new {o.User, o.Organization});
        });

    }
}

If i remove the OnModelCreating code, the migrations are created succesfully.

EDIT: As mentioned in the comments, the problem was that i was referencing the class and not a property that had the key of the entities

As requested, here is my solution:

public class OrganizationUnitMember
{
    public int OrganizationUnitMemberId { get; set; }
    public int UserId { get; set; }
    public int OrganizationUnitId { get; set; }
    [ForeignKey("UserId")]
    public virtual OmegaUser User { get; set; }
    [ForeignKey("OrganizationUnitId")]
    public virtual OrganizationUnit OrganizationUnit { get; set; }
    public int RoleId { get; set; }
    [ForeignKey("RoleId")]
    public virtual OrganizationRole Role { get; set; }
}

And the DbContext:

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<OrganizationUnit>(
            orgUnit =>
            {
                orgUnit.HasOne(ou => ou.Parent)
                    .WithMany(ou => ou.Children)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasForeignKey(ou => ou.ParentId);
            });
        builder.Entity<OrganizationUnitMember>(member =>
        {
            member.HasAlternateKey(m => new {m.OrganizationUnitId, m.UserId});
        });

    }

I had to add the ids of the referenced entities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM