MembershipUser 和实体框架代码优先

发布于 2024-12-01 22:16:53 字数 1084 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2024-12-08 22:16:53

可以使用适配器模式来解决这个问题。

public class CustomUser : MembershipUser
{
    public CustomUser(string providername,
                    User userAccount) :
        base(providername,
                        userAccount.UserName,
                        userAccount.Id,
                        userAccount.Email,
                        passwordQuestion,
                        string.Empty,
                        true,
                        false,
                        userAccount.CreationDate,
                        userAccount.LastLoginDate,
                        userAccount.LastActivityDate,
                        new DateTime(),
                        new DateTime())
    {
        UserAccount = userAccount;
    }

    public User UserAccount { get; private set; }
}

根据实体模型的内容自定义传递给基本构造函数的参数。

You can use the Adapter Pattern to solve this problem.

public class CustomUser : MembershipUser
{
    public CustomUser(string providername,
                    User userAccount) :
        base(providername,
                        userAccount.UserName,
                        userAccount.Id,
                        userAccount.Email,
                        passwordQuestion,
                        string.Empty,
                        true,
                        false,
                        userAccount.CreationDate,
                        userAccount.LastLoginDate,
                        userAccount.LastActivityDate,
                        new DateTime(),
                        new DateTime())
    {
        UserAccount = userAccount;
    }

    public User UserAccount { get; private set; }
}

Customize the parameters passed to the base constructor depending on what your entity model has.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文