Thursday, October 24, 2013

How Add a new attribute to an Entity Framework 5 model class(Data Base First)

I have entity class which was generated from entity framework 5 db first approach.So I needed to add another property to this class because I need to do few track the instance state for some reason.

There are few ways to this in Entity framework.

Approach 1 :Because entity class is partial can created another class same, same name space and add the new  property

Approach 2 : Add new property to directly to the entity and marked its as not mapped entity using NotMapped attribute.

In my scenario I used approach  2.

 

        public string Warehouse { get; set; }
public string ProductGroup { get; set; }
public string RolPackageNo { get; set; }

public virtual ICollection<Tab_AODLines> Tab_AODLines { get; set; }
public virtual ICollection<Tab_PickListLines> Tab_PickListLines { get; set; }
public virtual ICollection<Tab_SplitLines> Tab_SplitLines { get; set; }

//new property
[NotMapped]
public bool HasChanged { get; set; }



Of course you'll need to have these two using statements at the top your Entity Framework class to get things to compile.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

 

Bingoooooo Smile

No comments:

Post a Comment