代码之家  ›  专栏  ›  技术社区  ›  Whirlwind

使用实体框架6创建计算属性

  •  0
  • Whirlwind  · 技术社区  · 7 年前

    我使用数据库优先的方法,并从数据库创建了模型。现在,我在Winforms应用程序中有一个datagrid视图,它绑定到绑定源。所有这些都可以正常工作(datagrid视图中显示了适当的数据)。现在的问题是,如何添加由两个值(已在db中找到)组成的计算属性?例如:

    假设我有一个表用户(id、用户名、first\u name、last\u name、user\u type),但我希望在 绑定的datagrid视图 ,我希望有以下列:

    username, full name, type
    

    哪里 "full name" 我会得到什么 first_name + " " + last_name .

    我想我不能像这样手动修改模型类:

    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
        protected set {}
    }
    

    因为这个类是自动生成的,每次从现有数据库生成模型时(当我做一些更改时),我的代码都会被删除,所以这不是真正的选择。。。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Whirlwind    7 年前

    实际上,我通过使用部分类功能解决了这个问题: 我已经创建了另一个文件,其中包含我的用户模型类的另一部分(带有我的附加属性),一切都很顺利。

    namespace User.Model
    {
        public partial class User
        {
            public string FullName
            {
                get
                {
                    return (this.firstName + " " + this.lastName;
                }
                protected set { }
            }
        }
    }
    

    现在,当我生成模型类时,这个新文件不受EF的影响。datagrid视图也正确显示了此新字段。。。

        2
  •  0
  •   DasSoftware    7 年前

    我仍然不能添加评论,所以我将不得不这样做。

    关于您的方法,为什么不创建一个将绑定到数据网格视图的数据传输模型呢?

    使用这种方法,新模型将具有所需的属性FullName,您可以在网格视图中显示它。您的数据库实体模型将保持不变。通过这种方式,您已经将数据库模型与视图模型解耦,并实现了所需的功能。

    更新:

    /// <summary>
    ///     Assuming this is the EF model, generated with the database first approach. We leave it as is.
    /// </summary>
    public class UserEntityModel
    {
        public int Id { get; set; }
    
        public string UserName { get; set; } 
    
        public string FirstName { get; set; }
    
        public string LastName { get; set; }
    
        public int UserType { get; set; }
    }
    
    /// <summary>
    ///     This model represents your grid presentation specific data. As you can see, we have the fields 
    ///     that we will show. This is the new DTO model
    /// </summary>
    public class UserGridViewModel
    {
        public string UserName { get; set; }
    
        public string FullName { get; set; } 
    
        public int UserType { get; set; }
    }
    
    /// <summary>
    ///     This method demonstrates the retrieving and model to model mapping.
    /// </summary> 
    public UserGridViewModel GetUser(int userId)
    {
          //retrieve the UserEntityModel
          var efObject = _context.User.Where(user => user.Id == userId).SingleOrDefault();
    
           if (efObject  == null) {
              return null;
           }
    
           // construct the new object, set the required data 
           return new UserGridViewModel()
           {
                UserName = efObject.UserName,
                UserType = efObject.UserType,
                FullName = $"{efObject.FirstName} {efObject.LastName}"
            };
    }
    

    其他说明: 让我们假设 UserEntityModel 是数据库第一个生成的数据模型。 我们将保持原样。

    我们将创建第二个模型, UserGridViewModel 仅包含将显示在网格中的数据。这是DTO模型。

    GetUser方法应该在概念上演示第一个(ef模型)和第二个(DTO)模型的用法。我们从数据库中检索数据,构建DTO模型并将其传递给网格。

    您可以找到更多信息 here here .

    希望这有帮助,干杯,快乐编码!