ASP.NET MVC 5 identity application user as foreign key

牧云@^-^@ 提交于 2019-12-03 14:58:37

UserManager uses it's own DbContext to load data from the database. You will need to retrieve the user from the same dbContext that you use to add a reference to. Something like:

var currentUMUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());
var currentUser = db.Users.Find(currentUMUser.UserID);
retailer.UserProfile = currentUser;
db.Retailers.Add(retailer);
await db.SaveChangesAsync();
return RedirectToAction("Index");

Of course, you will have to retrieve the user from your DB using whatever method actually works for your models and DbContext.

Chuck Montana

Add the using statement:

using Microsoft.AspNet.Identity;

Then you can retrieve the UserId with:

var a = User.Identity.GetUserId();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!