实体框架代码优先:如何映射私有字段?

发布于 2024-10-24 14:57:38 字数 1499 浏览 1 评论 0原文

是否可以将表列映射到类字段而不是类属性以及如何映射?

你可以做到:)

请点击此链接:http://weblogs.asp.net/ricardoperes/archive/2013/08/22/mapping-non-public-members-with-entity-framework-code-first.aspx

这个这是一个常见的要求,而且确实有道理;我们需要使用 LINQ 表达式和一些反射魔法。首先,用于返回指向成员的表达式的辅助函数:

      public static class ExpressionHelper
      {
          public static Expression<Func<TEntity, TResult>> GetMember<TEntity, TResult>(String memberName)
          {
              ParameterExpression parameter = Expression.Parameter(typeof(TEntity), "p");
              MemberExpression member = Expression.MakeMemberAccess(parameter, typeof(TEntity).GetMember(memberName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single());
              Expression<Func<TEntity, TResult>> expression = Expression.Lambda<Func<TEntity, TResult>>(member, parameter);
              return (expression);
          }
     }

然后,我们在 DbContext.OnModelCreating 方法上调用它,作为 StructuralTypeConfiguration.Property 的参数:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity<Project>().Property(ExpressionHelper.GetMember<Project, Decimal>("Budget")).IsRequired();

      base.OnModelCreating(modelBuilder);
  }

Is it possible to map a table column to a class field instead to a class property and how?

YOU CAN DO IT :)

Follow this link: http://weblogs.asp.net/ricardoperes/archive/2013/08/22/mapping-non-public-members-with-entity-framework-code-first.aspx

This is a common request, and really makes sense; we need to use LINQ expressions and a bit of reflection magic. First, an helper function for returning an expression that points to a member:

      public static class ExpressionHelper
      {
          public static Expression<Func<TEntity, TResult>> GetMember<TEntity, TResult>(String memberName)
          {
              ParameterExpression parameter = Expression.Parameter(typeof(TEntity), "p");
              MemberExpression member = Expression.MakeMemberAccess(parameter, typeof(TEntity).GetMember(memberName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single());
              Expression<Func<TEntity, TResult>> expression = Expression.Lambda<Func<TEntity, TResult>>(member, parameter);
              return (expression);
          }
     }

Then, we call it on the DbContext.OnModelCreating method, as a parameter to StructuralTypeConfiguration.Property:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity<Project>().Property(ExpressionHelper.GetMember<Project, Decimal>("Budget")).IsRequired();

      base.OnModelCreating(modelBuilder);
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迟到的我 2024-10-31 14:57:38

实体框架(无论是否为 Code First)不支持映射到字段;仅限属性。

更新
正如评论中指出的,这些文档有点过时,但仍可能对任何初学者有所帮助:

实体框架代码优先开发资源和文档< /a>


为了完整起见,下面是 EF 4.1 RC 中包含的内容的链接:EF 4.1 候选版本可用

自 CTP5 以来的更改(来自上面的链接):

  • 将“DbDatabase”重命名为“Database”。这个班级也搬家了
    到“System.Data.Entity”
    命名空间以及数据库
    初始化类。


  • 将“ModelBuilder”重命名为“DbModelBuilder”,以与
    其他核心类。


  • 模型优先和数据库优先的验证。新的验证
    仅在代码中支持该功能
    首先是CTP5。在 RC 中验证
    该功能适用​​于所有三个
    开发工作流程(模型优先,
    数据库优先,代码优先)。

  • 完整的 Intellisense 文档。功能 CTP 未得到广泛记录
    因为 API 界面正在发生变化
    每个版本之间的差异显着。
    此版本包括完整的
    文档。

  • 删除代码优先可插入约定。可插入约定
    已在功能 CTP5 中预览,但
    未达到上线质量
    发布。此版本仍然支持
    删除默认约定。

  • 在 Code First 关系 API 中整合 IsIndependent。什么时候
    在功能中配置关系
    CTP5使用了IsIndependent方法
    以确定这种关系确实
    没有外键属性
    暴露在对象模型中。这是
    现在通过调用 Map 方法来完成。
    HasForeignKey 仍用于
    外键的关系
    属性暴露在对象中
    型号。


Entity Framework (Code First or not) does not support mapping to a field; only to properties.

UPDATE
As pointed out in the comments, these documents are a bit dated but might still help any beginner along:

Entity Framework Code first development Resources and Documentation

For the sake of completeness, heres a link to whats included in EF 4.1 RC: EF 4.1 Release Candidate Available

Changes since CTP5 (From the link above):

  • Rename of ‘DbDatabase’ to ‘Database’. This class has also moved
    to the ‘System.Data.Entity’
    namespace, along with the database
    initializer classes.

  • Rename of ‘ModelBuilder’ to ‘DbModelBuilder’, to align with the
    other core classes.

  • Validation in Model First and Database First. The new validation
    feature was only supported in Code
    First in CTP5. In RC the validation
    feature will work with all three
    development workflows (Model First,
    Database First, and Code First).

  • Complete Intellisense docs. Feature CTPs were not extensively documented
    because the API surface was changing
    significantly between each release.
    This release includes complete
    documentation.

  • Removal of Code First Pluggable Conventions. Pluggable Conventions
    were previewed in Feature CTP5 but
    were not at go-live quality for this
    release. This release still supports
    the removal of default conventions.

  • Consolidation of IsIndependent in the Code First relationship API. When
    configuring relationships in Feature
    CTP5 the IsIndependent method was used
    to identify that the relationship did
    not have a foreign key property
    exposed in the object model. This is
    now done by calling the Map method.
    HasForeignKey is still used for
    relationships where the foreign key
    property is exposed in the object
    model.

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