如何将simplememmembership UserProfile链接到带有外键的扩展概要文件
本文关键字:扩展 文件 simplememmembership UserProfile 链接 | 更新日期: 2023-09-27 18:01:23
我已经用尽了关于如何使这段代码以我想要的方式工作的想法。我有3个模型如下所示。
< UserProfile and Player
一对一的关系/strong>
UserId
与即将创建的玩家相同。当我按原样尝试代码时,没有响应,也没有添加或保存到数据库。
Team and Player
一对多关系
不言自明,玩家必须属于一个团队。这使得重定向到Edit操作不再像Player.TeamId != null
那样进行。
UserProfile
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public virtual Player Player { get; set; }
}
public class Team
{
[Key]
[HiddenInput(DisplayValue = false)]
public int TeamId { get; set; }
[Display(Name = "Full Name:")]
public string Name { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
玩家
public class Player
{
[Key]
[ForeignKey("UserProfile")]
[HiddenInput(DisplayValue = false)]
public int UserId { get; set; }
[Required]
[Display(Name = "Full name")]
public string FullName{ get; set; }
[Display(Name = "Team")]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
SavePlayer
public void SavePlayer(Player player)
{
using (var context = new EFootballDb())
{
if (player.UserId == 0)
{
context.Players.Add(player);
}
else if (player.UserId > 0)
{
var currentPlayer = context.Players
.Single(t => t.UserId == player.UserId);
context.Entry(currentPlayer).CurrentValues.SetValues(player);
}
context.SaveChanges();
}
}
创建动作
[HttpGet]
public ActionResult Create()
{
PopulateTeamsDropDownList();
return View();
}
[HttpPost]
public ActionResult Create(Player model)
{
try
{
if (ModelState.IsValid)
{
_dataSource.SavePlayer(model);
return RedirectToAction("Detail", "Team", new { id = model.TeamId });
}
}
catch (Exception)
{
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateTeamsDropDownList(model.TeamId);
return View(model);
}
private void PopulateTeamsDropDownList(object selectedTeams = null)
{
var teamsQuery = from d in _dataSource.Teams
orderby d.Name
select d;
ViewBag.TeamID = new SelectList(teamsQuery, "TeamId", "Name", selectedTeams);
}
在Team and Player
之间一切都很好,但我只是不能让UserProfile and Player
在创建时连接起来。有没有人有我应该尝试的想法或不同的方法?提前感谢!
更新:我正在添加另一种方法来做到这一点。这是一个非常复杂的方法,它可以工作,但最终你不会想要这个。
这是一个痛苦。我也花了很多时间来解决这个问题。我找到的最近的在线资源在这里。
注意那一页有两个错误。1)使用添加迁移而不是创建迁移2)当更新UsersContext时,确保包含DbSet的泛型类型。即
public DbSet<Membership> Membership { get; set; }
按照链接上的步骤操作。那么你需要再走一点。
对于我的解决方案,我将所有模型移动到模型类库,并将UsersContext移动到DAL类库,但这不是必需的。
一旦你完成了迁移,你可以添加一个基本类来链接到UserProfile,像这样…
public class User
{
public int UserId { get; set; }
public string FirstName { get; set; }
public int UserProfileId { get; set; }
[ForeignKey("UserProfileId")]
public UserProfile UserProfile { get; set; }
}
不要忘记更新UsersContext。注意:您可以将UsersContext重命名为任何您喜欢的名称。
public DbSet<User> Users { get; set; }
最后一步是添加一个在你的AccountController中调用的类来代替WebSecurity.CreateUserAndAccount(…)
public class CustomSimpleMembershipProvider
{
private readonly UsersContext _db = new UsersContext();
public bool CreateUser(string username, string password, string firstName)
{
var userCreated = false;
WebSecurity.CreateUserAndAccount(username, password);
var userProfile = _db.UserProfiles.FirstOrDefault(u => u.UserName == username);
// Check if user already exists
if (userProfile != null)
{
var member = new Model.User { FirstName = firstName, UserProfile = userProfile };
try
{
_db.Users.Add(member);
_db.SaveChanges();
userCreated = true;
}
catch (Exception)
{
//Add logging or throw whatever exceptions needed
throw;
}
}
return userCreated;
}
}
迁移的原因是它允许您在自己的上下文中控制成员表。我曾尝试在不同的环境中创建它们,但没有成功。我希望这能满足你的需求。
更简单的方法是创建一个带有UserProfile的虚拟外键的类。Virtual用于延迟加载。我这样做的原因是,一旦我得到了OAuth,它工作起来就容易多了。
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int UserProfileId { get; set; }
[ForeignKey("UserProfileId")]
public virtual UserProfile UserProfile { get; set; }
}
然后把你的UsersContext从AccountModels.cs移到你的自定义Context中,像这样。
public class OAuthNoMigrateContext : DbContext
{
public OAuthNoMigrateContext()
: base("DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
创建一个与我的另一个答案类似的CustomSimpleMembershipProvider。
public class CustomSimpleMembershipProvider
{
private readonly OAuthNoMigrateContext _db = new OAuthNoMigrateContext();
public bool CreateUser(string username, string password, User user)
{
var userCreated = false;
WebSecurity.CreateUserAndAccount(username, password);
var userProfile = _db.UserProfiles.FirstOrDefault(u => u.UserName == username);
// Check if user exists
if (userProfile != null)
{
user.UserProfile = userProfile;
try
{
_db.Users.Add(user);
_db.SaveChanges();
userCreated = true;
}
catch (Exception)
{
//Add logging or throw whatever exceptions needed
throw;
}
}
return userCreated;
}
}
添加到Application_Start
Database.SetInitializer(new OAuthNoMigrateInitializer());
注意:我喜欢创建一个方法来检查用户,并从Application_Start调用。初始化是在你第一次调用DB的时候开始的。
private static void ViewDb()
{
using (var context = new OAuthNoMigrateContext())
{
var user = context.Users.Where(u => u.FirstName.StartsWith("T"));
}
}
调用这个初始化式
public class OAuthNoMigrateInitializer : DropCreateDatabaseAlways<OAuthNoMigrateContext>
{
private readonly CustomSimpleMembershipProvider _provider = new CustomSimpleMembershipProvider();
protected override sealed void Seed(OAuthNoMigrateContext context)
{
//Initialize UserProfile
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
AddUsers(context);
}
private void AddUsers(OAuthNoMigrateContext context)
{
if (WebSecurity.UserExists("username")) return;
var user = new User
{
FirstName = "FName",
LastName = "LName"
};
_provider.CreateUser("username", "password", user);
}
}