【问题标题】:Creating a computed property with Entity Framework 6使用 Entity Framework 6 创建计算属性
【发布时间】:2018-08-12 08:16:20
【问题描述】:

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

假设我有一个表用户(id、username、first_name、last_name、user_type),但我想在我的绑定数据网格视图中有不同的列,并且我想要这些列:

username, full name, type

"full name" 是我用first_name + " " + last_name 得到的结果。

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

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

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

【问题讨论】:

    标签: c# winforms entity-framework-6 ef-database-first computed-properties


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我仍然无法添加 cmets,所以我将不得不这样做。

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

      使用这种方法,您的新模型将具有所需的属性 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 模型并将其传递给网格。

      您可以找到更多信息herehere

      希望这对您有所帮助,祝您编码愉快!

      【讨论】:

      • 嗯,当我阅读第二个链接时,我想我明白了 :) 我会试试这个,然后告诉你结果如何。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2014-02-07
      • 1970-01-01
      • 2015-11-23
      • 2015-10-22
      • 2015-01-15
      相关资源
      最近更新 更多