MembershipUser 和实体框架代码优先
我正在使用 EF Code First,现在在实现自定义 MembershipProvider 时遇到了麻烦。
对于 EF Code First,我创建了自己的用户类,如下所示:
public class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string Email { get; set; }
[Required]
public bool IsApproved { get; set; }
public DateTime? LastLoginDate { get; set; }
public DateTime? LastActivityDate { get; set; }
[Required]
public DateTime CreationDate { get; set; }
[Required]
public string ApplicationName { get; set; }
}
我还已经为 EF 实现了自定义 MembershipProvider 的一些函数,但其中一些函数现在需要 MembershipUser
作为参数或返回值。
public MembershipUser GetUser(string username, bool userIsOnline)
我的第一个想法是从 MembershipUser
继承我的 User 类,但随后我失去了对 Properties 的控制。这对 EF Code First 也适用吗?
另一种想法是为我的用户类创建一个 ToMembershipUser() 方法。这是一个选择吗?我必须考虑什么?
处理这个问题的最佳方法是什么?
I am playing around with EF Code First and now ran into trouble when implementing a custom MembershipProvider.
For EF Code First I created my own user class like this:
public class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
[Required]
public string Email { get; set; }
[Required]
public bool IsApproved { get; set; }
public DateTime? LastLoginDate { get; set; }
public DateTime? LastActivityDate { get; set; }
[Required]
public DateTime CreationDate { get; set; }
[Required]
public string ApplicationName { get; set; }
}
I also implemented some functions of my custom MembershipProvider for EF already, but some of them now require MembershipUser
as either a parameter or a return value.
public MembershipUser GetUser(string username, bool userIsOnline)
My first thought was to inherit my User class from MembershipUser
, but then I lose the control over the Properties. Would that even work with EF Code First?
An alternate idea is to create a ToMembershipUser() method for my user class. Would that be an option? What would I have to consider?
What's the best way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用适配器模式来解决这个问题。
根据实体模型的内容自定义传递给基本构造函数的参数。
You can use the Adapter Pattern to solve this problem.
Customize the parameters passed to the base constructor depending on what your entity model has.