1

I am creating a custom table and using Identity. I have a context for adding rows to this table aswell.

code:

public class UserRoleAccess
{
    [Key]
    public string Id { get; set; }

    public string UserRoleId { get; set; }

    [ForeignKey("Id")]
    public virtual ApplicationRole ApplicationRole { get; set; }

    public bool IsActive { get; set; }

    public string Name { get; set; }

    public int UserRoleAccessType { get; set; }
}

db context:

public class UserRoleAccessContext : DbContext
{
    public UserRoleAccessContext() : base("DMConnection")
    {
        Database.SetInitializer<PersonContext>(null);
        Configuration.LazyLoadingEnabled = false;
        Configuration.ProxyCreationEnabled = false;
    }

    public static UserRoleAccessContext Create()
    {
        return new UserRoleAccessContext();
    }

    public DbSet<UserRoleAccess> UserRoleAccess { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        // IMPORTANT: we are mapping the entity User to the same table as the entity ApplicationUser
        //modelBuilder.Entity<IdentityUserLogin>().ToTable("Users").HasKey(e => e.UserId);

    }

    public DbQuery<T> Query<T>() where T : class
    {
        return Set<T>();
    }
}

bindings:

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();


        modelBuilder.Entity<ApplicationUser>().HasRequired(p => p.Person)
            .WithMany(b => b.Users);

        modelBuilder.Entity<ApplicationUser>().HasRequired(p => p.Person)
            .WithMany(b => b.Users)
            .HasForeignKey(p => p.PersonId);

        modelBuilder.Entity<UserRoleAccess>().HasRequired(p => p.ApplicationRole)
                    .WithMany(b => b.UserRoleAccess);

        modelBuilder.Entity<UserRoleAccess>().HasRequired(p => p.ApplicationRole)
            .WithMany(b => b.UserRoleAccess)
            .HasForeignKey(p => p.UserRoleId);

    }

Executing this code when I get an error:

    public static void CreateUserRoleAccess(string name, int type, string id)
    {
        var userAccessContext = new UserRoleAccessContext();
        var userRoleAccess = new UserRoleAccess();

        userRoleAccess.UserRoleId = id;
        userRoleAccess.IsActive = true;

        userRoleAccess.UserRoleAccessType = type;

        userRoleAccess.Name = name;

        userAccessContext.UserRoleAccess.Add(userRoleAccess);
    }

error:

One or more validation errors were detected during model generation:

Project.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

I assume I am missing a binding somewhere but I can't figure out where?

EDIT: I changed UserRoleAccess class. I changed the Id from string to long and I tried this in TSQL

  INSERT INTO UserRoleAccess (UserRoleId,IsActive, Name, UserRoleAccessType) VALUES('e5b2e76a-a106-4076-b219-6987411995e7', 1, 'TEST', 1)

It worked. Although Im still getting the same error when running it from my application

EDIT 2 added this in my bindings :

 modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
 modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

Still the same result

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94
  • It says your class `Project.Models.IdentityUserRole` does not have key defined. Can you show class definition? – Red Oct 10 '16 at 13:29
  • it is the built in class: https://msdn.microsoft.com/en-us/library/dn613252(v=vs.108).aspx – ThunD3eR Oct 10 '16 at 13:31
  • Possible duplicate of [User in Entity type MVC5 EF6](http://stackoverflow.com/questions/19913447/user-in-entity-type-mvc5-ef6) – Red Oct 10 '16 at 13:35
  • tried that, did not work. – ThunD3eR Oct 10 '16 at 13:38
  • `EntityType 'xxxx' has no key defined` error often caused from not all tables have primary key defined by `KeyAttribute` on the model. Set `KeyAttribute` on all of the table partial classes' primary key and ensure the bindings work fine. – Tetsuya Yamamoto Oct 10 '16 at 15:23

1 Answers1

0

So funny thing is.

My second edit was actually correct. but for some reason it did not work when I migrated and updated db with code first.

I deleted all the tables and created them again via migrations and it work.

Although with the edit I had to put this code in both db contexts:

        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
ThunD3eR
  • 3,216
  • 5
  • 50
  • 94