Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First: How to map private fields?

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);
  }
like image 566
mynkow Avatar asked Mar 21 '11 12:03

mynkow


1 Answers

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.

like image 117
Sergi Papaseit Avatar answered Sep 27 '22 02:09

Sergi Papaseit