User in Entity Type MVC5 Ef6

User in Entity type MVC5 EF6

The following method, OnModelCreating, will create a working context. Not sure if it is the mapping you want though.

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

modelBuilder.Entity<Content>()
.HasMany(c => c.Editors)
.WithOptional()
.WillCascadeOnDelete(false);

modelBuilder.Entity<Content>()
.HasRequired(c => c.Owner)
.WithOptional()
.WillCascadeOnDelete(false);

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 });
}

Entity type has no key defined EF6

If you are using EF Code First (not specified in your question), you will need to change the ContactId property name to ContactsId to match the convention of ClassName + Id in order to define the key for your Contacts entity type.

See the MSDN Code First Conventions: http://msdn.microsoft.com/en-us/data/jj679962.aspx

MVC 5 Entity Framework 6 Getting all user in a role

Ok so it was a brain dead moment all I needed to do was the following:

var customers = db.AspNetUsers
.Where(u => u.AspNetRoles.Any(r => r.Name == "Customer") &&
u.IsActivated == true && u.IsClosed == false &&
u.IsPaused == false && u.IsSuspended == false)
.ToList();

how to get users list depending on their roles in Entity Framework 6

Its solved using the following code

in Controller

string DoctorRoleID = roleManager.FindByName("Doctor").Id;
ViewBag.DoctorID = new SelectList(db.Users.Where(u => u.Roles.Any(r => r.RoleId == DoctorRoleID)).ToList(), "Id", "Email");

In View

<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(model => model.DoctorID, "DOCTOR", htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<div class="col-md-10">
@Html.DropDownList("DoctorID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EndUserID, "", new { @class = "text-danger" })
</div>
</div>


Related Topics



Leave a reply



Submit