实体框架代码首先:如何映射私有字段? [英] Entity Framework Code First: How to map private fields?

查看:103
本文介绍了实体框架代码首先:如何映射私有字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

您可以做到:

请按照以下链接: http://weblogs.asp.net/ricardoperes/archive/2013/08/22/mapping-non-public-members-with -entity-framework-code-first.aspx

这是一个常见的请求,真的有道理;我们需要使用LINQ表达式和一点反思魔法。首先,一个帮助函数返回一个指向一个成员的表达式:

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);
          }
     }

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

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);
  }


推荐答案

实体框架不)不支持映射到字段;只有属性。

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:


实体框架代码首先开发资源和文档

为了这个缘故的完整性,继承了包含在EF 4.1 RC中的链接: EF 4.1发布候选人可用

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

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

Changes since CTP5 (From the link above):



  • 将DbDatabase重命名为数据库。此类还将
    移动到'System.Data.Entity'
    命名空间,以及数据库
    初始化程序类。

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

将ModelBuilder重命名为DbModelBuilder,以与
其他核心​​类对齐。

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

首先和数据库中的验证。新的验证
功能仅在CTP5中的Code
中首先支持。在RC中,验证
功能将适用于所有三个
开发工作流(Model First,
数据库优先和代码优先)。

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).

完整的Intellisense文档。功能CTP没有广泛记录
,因为API表面在每个版本之间显着更改

此版本包含完整的
文档。

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

删除代码第一个可插拔约定可插入约定$ b功能CTP5中预览了$ b,但

版本中没有出现。此版本仍然支持
删除默认约定。

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.

在Code First关系API中合并IsIndependent 。当
在Feature
CTP5中配置关系时,使用IsIndependent方法
来确定关系在
中没有在对象模型中暴露的外键属性
。现在通过调用Map方法完成

HasForeignKey仍然用于
关系,其中外键
属性在对象
模型中公开。

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.

这篇关于实体框架代码首先:如何映射私有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆