Entity Framework Code First: How to map private fields?

Is it possible to map a table column to a class field instead of 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 general request and really makes sense; we need to use LINQ expressions and some reflection magic. First, a helper function to return 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 the DbContext.OnModelCreating method as a parameter for StructuralTypeConfiguration.Property:

  protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Project>().Property(ExpressionHelper.GetMember<Project, Decimal>("Budget")).IsRequired(); base.OnModelCreating(modelBuilder); } 
+7
source share
1 answer

Entity Framework (first code or not) does not support display in a field; only for properties.

UPDATE As stated in the comments, these docs are a bit outdated, but can help any newbie:

Primary Entity Framework Code Development and Documentation

For completeness, heres a link to what is included in EF 4.1 RC: Applicant available for EF 4.1

Changes with CTP5 (at the link above):

  • Rename the DbDatabase to '. This class also moved to the 'System.Data.Entity namespace along with the database initializer classes.

  • Rename 'ModelBuilder to' DbModelBuilder to match with other core classes.

  • Verification at the beginning of the model and database. The new feature check was only supported in code First in CTP5. In RC validation, the function will work with all three development workflows (Model First, First database and first code).

  • Complete Intellisense Documents . CTPs were not widely documented because the API surface varied significantly between each release. This release includes complete documentation.

  • Removing the first connected codes. The plug-in agreements were viewed in Feature CTP5, but were not in good quality for this release. This release still supports removal of default conventions.

  • Consolidation of IsIndependent in the Code First API. when setting relations in the CTP5 function, the IsIndependent method was used to determine that the relations do not have foreign key property set in the object model. This is now accomplished by calling the Map method. HasForeignKey is still used for relationships in which the foreign key property is set in the model object.

+4
source

All Articles