MVC4模型自定义功能

时间:2014-01-07 12:08:47

标签: asp.net-mvc-4

抱歉新手问题,我是MVC和OOP的新手

我的USER数据库表格有以下模型

namespace MyApp.Models
{
    public class User
    {
        public int user_id { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string salt { get; set; }
        public string email { get; set; }
        public sbyte status { get; set; }
        public System.DateTime creation_date { get; set; }
        public sbyte type { get; set; }
        public virtual Doctor Doctor { get; set; }
        public virtual Owner Owner { get; set; }
        public virtual UserToken UserToken { get; set; }
        public virtual Veterinarian Veterinarian { get; set; }

    }
}

实际上,为了根据邮件或id调用特定的USER,我使用一个名为CustomDbFunctions的特定类

namespace MyApp.Models.DAL
{
    public static class CustomDbFunctions
    {
        public static User GetUserEntityFromEmail(string email, DbContext db)
        {
            return db.Users.FirstOrDefault(u => u.email == (string)email);
        }
    }
}

以这种方式在我的代码中使用

User user = CustomDbFunctions.GetUserEntityFromEmail(email, db)

这对我100%好,但我不知道这种方法是否正确,或者是否有更好的方式

//select the single user by calling only the class USER    
User mySelectedUser = new User(email)

非常感谢。

1 个答案:

答案 0 :(得分:0)

了解如何访问MVC4应用程序中的数据,您可以阅读本教程from the Asp.Net MVC main page。阅读有关MVC4的整个教程,您将对如何使用它有一个坚实的想法。

但我也推荐本教程使用良好的Entityframework设计模式,它叫做Repository Pattern,我只是一个很好的方法来获取所有代码(就像所有其他模式一样)。让我知道。

相关问题