Entity Framework automatically inserts new row in parent table - c#

I'm using an UI to insert data into the DB. Here is my entity model and controller. Whenever I'm inserting data into the UserDetail table, it automatically creates a new row in the UserRole table. Not sure why it's happening like this. For now UserRole is hard-coded in controller.
public class UserDetail
{
[Key]
public int UserID { get; set; }
[Required]
[StringLength(30, MinimumLength = 4)]
public string UserName { get; set; }
[Required]
[StringLength(50, MinimumLength = 4)]
public string FirstName { get; set; }
[Required]
[StringLength(50, MinimumLength = 4)]
public string LastName { get; set; }
[Required]
[EmailAddress]
[StringLength(150, MinimumLength = 4)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(30,MinimumLength=4)]
public string Password { get; set; }
public UserRole UserRole { get; set; }
}
public class UserRole
{
[Key]
public int RoleID { get; set; }
[Required]
[StringLength(20,MinimumLength=5)]
public string RoleName { get; set; }
public IEnumerable<UserDetail> UserDetail { get; set; }
}
[HttpPost]
public HttpResponseMessage Register(UserDetail usrInfo)
{
UserContext ctx = new UserContext();
UserDetail user = new UserDetail
{
UserRole = ctx.UserRole.Where(id => id.RoleID == 2).Select(r => r).FirstOrDefault(),
FirstName = usrInfo.FirstName,
LastName = usrInfo.LastName,
UserName = usrInfo.UserName,
Password = usrInfo.Password,
Email = usrInfo.Email,
};
_unitofwork.userDetail.Add(user);
if (_unitofwork.Completed() > 0)
return Request.CreateResponse(HttpStatusCode.OK, "Created");
else
return Request.CreateResponse();
}
public class UserContext: DbContext
{
public UserContext():base()
{
Database.SetInitializer<UserContext>(new CreateDatabaseIfNotExists<UserContext>());
//Database.SetInitializer<UserContext>(new DropCreateDatabaseIfModelChanges<UserContext>());
}
public DbSet<UserDetail> UserDetails { get; set; }
public DbSet<UserRole> UserRole { get; set; }
}

In the statement where you are instantiating the userdetail object you use a separately defined context to query for the user role:
UserContext ctx = new UserContext();
UserDetail user = new UserDetail
{
**UserRole = ctx.UserRole.Where(id => id.RoleID == 2).Select(r => r).FirstOrDefault(),**
FirstName = usrInfo.FirstName,
LastName = usrInfo.LastName,
UserName = usrInfo.UserName,
Password = usrInfo.Password,
Email = usrInfo.Email,
};
_unitofwork.userDetail.Add(user);
if (_unitofwork.Completed() > 0)
return Request.CreateResponse(HttpStatusCode.OK, "Created");
else
return Request.CreateResponse()
You then add the user to the userdetail collection under the _unitofwork object which has it's own context. To make thiswork, the userrole object you retrieved has to be under the same context to which you are adding the userdetail object.
So you probably want something like:
UserDetail user = new UserDetail
{
UserRole = _unitofwork.UserRole.Where(id => id.RoleID == 2).Select(r => r).FirstOrDefault(),
FirstName = usrInfo.FirstName,
LastName = usrInfo.LastName,
UserName = usrInfo.UserName,
Password = usrInfo.Password,
Email = usrInfo.Email,
};
_unitofwork.userDetail.Add(user);

Related

Add Full Identity Role List into Seperate Model

I am trying to add a full list of available roles into an edit user modal. I know how to only list the roles the user has; however, I want to provide the full list so that you can check/uncheck a checkbox on the list and update using "isSelected". I have tried many options found on here and across the internet; however, most of them leave me with a type mismatch (cannot convert Type to EditUserViewModel.Role). I attempted to do it the same way I pulled the roles for the specific user but it pulls "{Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1[Microsoft.AspNetCore.Identity.IdentityRole]}" as the Role and breaks it.
Method:
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
//GET USER INFORMATION - EXIT IF USER DOESN'T EXIST
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
ViewBag.ErrorMessage = $"User with Id = {id} cannot be found";
return View("NotFound");
}
//USER INFORMATION ---------------------------------------
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Title = user.Title,
Address = user.Address,
City = user.City,
State = user.State,
//CompanyId = user.CompanyId
};
// ViewBag.SelectedCommpany = user.CompanyId;
//COMPANY DROPDOWN INFO------------------------------------
var company = from c in companyRepository.GetCompanys() select c;
foreach (var c in company)
{
////Store this inforamtion into the company list in the viewmodel
var companyinfo = new EditUserViewModel.CompanyList
{
CompanyName = c.CompanyName,
CompanyID = c.CompanyId
};
model.CompanyLists.Add(companyinfo);
};
//GET LIST OF ROLES(RoleID, RoleName)
var roles = roleManager.Roles;
foreach (var RoleName in roles)
{
//Execute identity method to get full information for the Role and store into an object (roleinfo)
var roleString = Convert.ToString(roles);
var fullRoleInfo = await roleManager.FindByNameAsync(roleString);
//Store this inforamtion into the Role list in the viewmodel
var roleinfo = new EditUserViewModel.Role
{
RoleName = fullRoleInfo.Name,
RoleID = fullRoleInfo.Id
};
model.Roles.Add(roleinfo);
};
}
Model:
namespace PortalDev.Models.ViewModels
{
public class EditUserViewModel
{
public EditUserViewModel()
{
Claims = new List<Claim>();
Roles = new List<Role>();
//CompanyLists = new List<ICompanyRepository>();
CompanyLists = new List<CompanyList>();
}
//ROLES ---------------------------------------------
public class Role
{
public string RoleName { get; set; }
public string RoleID { get; set; }
public bool IsSelected { get; set; }
}
public List<Role> Roles { get; set; }
//CLAIMS----------------------------------------------
public class Claim
{
public string ClaimType { get; set; }
public string ClaimID { get; set; }
}
public List<Claim> Claims { get; set; }
//COMPANY DROPDOWN--------------------------------------
public class CompanyList
{
public string CompanyName { get; set; }
public int CompanyID { get; set; }
}
[Display(Name = "Company")]
public List<CompanyList> CompanyLists { get; set; } //List of Companies for dropdown
public string SelectedCompany { get; set; }
//USER INFORMATION --------------------------------------
public string Id { get; set; }
//[Required]
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
In the loop you are trying to convert the whole set of roles to the name of one role, instead using the individual role name instead. So just replace the line:
var roleString = Convert.ToString(roles);
with
var roleString = RoleName.Name;
An alternative approach using Linq could look like that:
var allRoleViewModels = roleManager.Roles.Select(x => new EditUserViewModel.Role {RoleID = x.Id, RoleName = x.Name}).ToList();

How do I make a relationship between my Aspnetusers table and a custom table?

I have extended my Aspnetusers table to include FirstName, MiddleName, LastName, StudentID.
I want a simple registation page where in the admin will create student accounts. The student accounts then should
be able to add their own personal contact details.
Now, I created a custom table named "ContactDetails" containing the ff:
public class Contact
{
[Key]
[Display(Name = "User Name")]
public string UserName { get; set; }
[Display(Name = "Contact Number")]
public string StudentContactNumber { get; set; }
[Display(Name = "Mother First Name")]
public string MotherFirstName { get; set; }
[Display(Name = "Mother Middle Name")]
public string MotherMiddleName { get; set; }
[Display(Name = "Mother Last Name")]
public string MotherLastName { get; set; }
[Display(Name = "Mother Contact Number")]
public string MotherContactNumber { get; set; }
[Display(Name = "Father First Name")]
public string FatherFirstName { get; set; }
[Display(Name = "Father Middle Name")]
public string FatherMiddleName { get; set; }
[Display(Name = "Father Last Name")]
public string FatherLastName { get; set; }
[Display(Name = "Father Contact Number")]
public string FatherContactNumber { get; set; }
[Display(Name = "Emergency First Name")]
public string EmergencyFirstName { get; set; }
[Display(Name = "Emergency Middle Name")]
public string EmergencyMiddleName { get; set; }
[Display(Name = "Emergency Last Name")]
public string EmergencyLastName { get; set; }
[Display(Name = "Emergency Contact Number")]
public string EmergencyContactNumber { get; set; }
public virtual ApplicationUser User { get; set; }
}
IdentityModels class:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string StudentID { get; set; }
public string College { get; set; }
public virtual System.Collections.Generic.ICollection<Contact> ContactInfo { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
How do I put my user(Aspnetusers.Id) in ContactDetails (User_Id) table when the student is registered?
How do I make a relationship between them?
My AdminController:
// GET: /Admin/Edit/Create
[Authorize(Roles = "Administrator")]
#region public ActionResult Create()
public ActionResult Create()
{
ExpandedUserDTO objExpandedUserDTO = new ExpandedUserDTO();
ViewBag.Roles = GetAllRolesAsSelectList();
return View(objExpandedUserDTO);
}
#endregion
// PUT: /Admin/Create
[Authorize(Roles = "Administrator")]
[HttpPost]
[ValidateAntiForgeryToken]
#region
public ActionResult Create(ExpandedUserDTO paramExpandedUserDTO)
{
try
{
if (paramExpandedUserDTO == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var Email = paramExpandedUserDTO.Email.Trim();
var UserName = paramExpandedUserDTO.Email.Trim();
var Password = paramExpandedUserDTO.Password.Trim();
//
var FirstName = paramExpandedUserDTO.FirstName.Trim();
var MiddleName = paramExpandedUserDTO.MiddleName.Trim();
var LastName = paramExpandedUserDTO.LastName.Trim();
var StudentID = paramExpandedUserDTO.StudentID.Trim();
var College = paramExpandedUserDTO.College.Trim();
// UserName is LowerCase of the Email
UserName = Email.ToLower();
// Create user
var objNewAdminUser = new ApplicationUser { UserName = UserName, Email = Email, FirstName = FirstName, MiddleName = MiddleName, LastName = LastName, StudentID = StudentID, College = College };
var AdminUserCreateResult = UserManager.Create(objNewAdminUser, Password);
if (AdminUserCreateResult.Succeeded == true)
{
string strNewRole = Convert.ToString(Request.Form["Roles"]);
if (strNewRole != "0")
{
// Put user in role
UserManager.AddToRole(objNewAdminUser.Id, strNewRole);
}
return Redirect("~/Admin");
}
else
{
ViewBag.Roles = GetAllRolesAsSelectList();
ModelState.AddModelError(string.Empty, "Error: User "+Email+ " already exists!");
//"Error: Failed to create the user. Check password requirements.");
return View(paramExpandedUserDTO);
}
}
catch (Exception ex)
{
ViewBag.Roles = GetAllRolesAsSelectList();
//ModelState.AddModelError(string.Empty, "Error: " + ex);
return View("Create");
}
}
#endregion
Im not sure how do I proceed? Thanks in advance.
In Contact class you should have these:
public int UserId { get; set; }
public virtual ApplicationUser User { get; set; }
In ApplicationUser class you should have these:
public int ContactId { get; set; }
public virtual Contact Contact { get; set; }

I have an error message "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" but not sure of suggested solutions

I am trying to add a user using aspnet.identity and get this error message on
IdentityResult result = manager.Create(user, Password.Text);
the code it is contained in
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
int x = int.Parse(Address.SelectedValue.ToString());
using (var db = new ApplicationDbContext())
{
physAddress = db.PhysAddress.Find(x);
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text, FirstName = FirstName.Text, LastName = LastName.Text, PhoneNumber = PhoneNumber.Text, HomePhone = HomePhone.Text, PhysAddressId = physAddress };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)....
I've tried researching error message but not sure how to apply in this example. Any suggestions would be appreciated.
I have a class defined as follows and was using db to get a row from that table using EF
public class PhysAddress
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PhysAddressId { get; set; }
[Required]
public string Street { get; set; }
[Required]
public string City { get; set; }
[Required]
public string State { get; set; }
[Required]
public string ZipCode { get; set; }
[Required]
And then below ApplicationDbContext in IdentityModels.cs have below
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<PhysAddress> PhysAddress { get; set; }
In IdentityModels have added for each user the property
public virtual PhysAddress PhysAddressId { get; set; }
I was able to resolve by using
((IObjectContextAdapter)db).ObjectContext.Detach(physAddress);

Pass a user to a ViewModel

OK, a little stuck here.
VIEWMODEL
public class UserProfileEdit
{
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public string FirstName { get; set; }
public string TwitterHandle{ get; set; }
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// etc etc
}
CONTROLLER
public ActionResult YourProfile()
{
string username = User.Identity.Name;
ApplicationUser user = db.Users.FirstOrDefault(u => u.UserName.Equals(username));
// Construct the viewmodel
UserProfileEdit model = new UserProfileEdit();
model.ApplicationUser = user;
return View(model);
}
And on the View I have #model MySite.Models.UserProfileEdit at the top.
How can I pass the user to the ViewModel? I know I can do it line by line
model.Email = user.Email;
for example, but it should be simpler?
You can do it line by line, or you can use AutoMapper. Give it a try http://automapper.org/
This is very useful specially when you have the same kind of object mappings repeated in your code.
You have several options to do what you want to do.
You can use a tool, like AutoMapper.
Or you can pass data by a constructor :
public class UserProfileEdit
{
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public string FirstName { get; set; }
public string TwitterHandle{ get; set; }
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// etc etc
public UserProfileEdit() {}
public UserProfileEdit(ApplicationUser user) {
this.ApplicationUser = user;
this.Email = user.Email;
// ...
}
}
public ActionResult YourProfile()
{
string username = User.Identity.Name;
ApplicationUser user = db.Users.FirstOrDefault(u => u.UserName.Equals(username));
return View(new UserProfileEdit(user));
}
Or using a method to init your view model's data :
public class UserProfileEdit
{
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public string FirstName { get; set; }
public string TwitterHandle{ get; set; }
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// etc etc
public void Init(ApplicationUser user) {
this.ApplicationUser = user;
this.Email = user.Email;
// do what you want to do
}
}
public ActionResult YourProfile()
{
string username = User.Identity.Name;
ApplicationUser user = db.Users.FirstOrDefault(u => u.UserName.Equals(username));
UserProfileEdit vm = new UserProfileEdit();
vm.Init(user);
return View(vm);
}

MVC 5 GetUserId() returns null

I have created a custom User class implementing IUser.
When I register everything completes fine, the tables are created (if they don't exist) and all looks ok, but, as the subject states, I cannot get User.GetUserId() to work.
It always returns null which is really annoying.
Here is my User class:
public class User : IUser
{
public User()
{
this.LastLoginDate = DateTime.UtcNow;
this.DateCreated = DateTime.UtcNow;
}
public User(string userName)
{
this.Id = Guid.NewGuid().ToString();
this.UserName = userName;
this.CreatedBy = this.Id;
this.LastLoginDate = DateTime.UtcNow;
this.DateCreated = DateTime.UtcNow;
this.IsApproved = true;
}
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredUserName")]
public string UserName { get; set; }
public string Id { get; set; }
[Required] public string CompanyId { get; set; }
[Required] public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public DateTime LastLoginDate { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredTitle")]
public string Title { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredFirstName")]
public string Forename { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredLastName")]
public string Surname { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredEmail")]
public string Email { get; set; }
public string JobTitle { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public string Photo { get; set; }
public string LinkedIn { get; set; }
public string Twitter { get; set; }
public string Facebook { get; set; }
public string Google { get; set; }
public string Bio { get; set; }
public string CompanyName { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredCredentialId")]
public string CredentialId { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "RequiredSecurityCode")]
public bool IsLockedOut { get; set; }
public bool IsApproved { get; set; }
[Display(Name = "Can only edit own assets")]
public bool CanEditOwn { get; set; }
[Display(Name = "Can edit assets")]
public bool CanEdit { get; set; }
[Display(Name = "Can download assets")]
public bool CanDownload { get; set; }
[Display(Name = "Require approval to upload assets")]
public bool RequiresApproval { get; set; }
[Display(Name = "Can approve assets")]
public bool CanApprove { get; set; }
[Display(Name = "Can synchronise assets")]
public bool CanSync { get; set; }
public bool AgreedTerms { get; set; }
public bool Deleted { get; set; }
}
public class UserContext : IdentityStoreContext
{
public UserContext(DbContext db)
: base(db)
{
this.Users = new UserStore<User>(this.DbContext);
}
}
public class UserDbContext : IdentityDbContext<User, UserClaim, UserSecret, UserLogin, Role, UserRole>
{
}
Here is my Register function:
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
try
{
// Create a profile, password, and link the local login before signing in the user
var companyId = Guid.NewGuid().ToString();
var user = new Skipstone.Models.User(model.UserName)
{
CompanyId = companyId,
Title = model.Title,
Forename = model.Forename,
Surname = model.Surname,
Email = model.Email,
CompanyName = model.CompanyName,
CredentialId = model.CredentialId
};
if (await IdentityStore.CreateLocalUser(user, model.Password))
{
// Create our company
var company = new Company()
{
Id = companyId,
CreatedBy = user.Id,
ModifiedBy = user.Id,
Name = model.CompanyName
};
using (var service = new CompanyService())
{
service.Save(company);
}
await AuthenticationManager.SignIn(HttpContext, user.Id, isPersistent: false);
var test = User.Identity.IsAuthenticated; // returns true
var u = User.Identity.GetUserId(); // returns null
return RedirectToAction("Setup", new { id = companyId });
}
else
{
ModelState.AddModelError("", "Failed to register user name: " + model.UserName);
}
}
catch (IdentityException e)
{
ModelState.AddModelError("", e.Message);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
var test returns true, so it knows the user is now logged in.
var u is null, so User.Identity.GetUserId() is not working as it should.
Does anyone have any idea why?
I can post more code on request :)
Update 1
By creating a fresh project and testing with defaults, I have found that called User.Identity returns null when in the Register method.
But in the fresh project, calling it anywhere else returns the right information, but in my project I get null for everything.
Update 2
It is definately related to the custom User class.
I created one on my fresh project and the same thing happens.
So, something is wrong :) now I just have to figure out what!?!
I assume somewhere in your project there is a row of database connection initialization, something like this:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true)
So, make sure that you use "Id" instead of "UserId" in this line

Categories