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
How to Solve Operator '!=' Cannot Be Applied to Operands of Type 'T' and 'T'
How to Insert a Data Table into SQL Server Database Table
Populate Treeview with File System Directory Structure
How to Add a Custom Routed Command in Wpf
How to Check If Object Already Exists in a List
Run Code Once Before and After All Tests in Xunit.Net
Itextsharp - Sending In-Memory PDF in an Email Attachment
Implementing Audit Log/Change History with MVC & Entity Framework
Create an Application Setup in Visual Studio 2013
ASP.NET MVC Dropdownlistfor with Model of Type List<String>
What Is the Correct Way to Read a Serial Port Using .Net Framework