user755404
user755404

Reputation:

Have a custom Setter on property for EF Code First model entity

Is it possible to override or add code to setter of property for entity model object in EF Code First approach.

e.g.

public class Contact
{
    [Key]
    public int Id { get; private set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string JobTitle
    {
        get;
        set { // eg. proper case the job title }
    }

}

I had tried having a public property marked NotMapped and this set/get to private/protected property that is. But it seems that the property has to public to be created in table.

Upvotes: 15

Views: 17528

Answers (3)

KarlZ
KarlZ

Reputation: 170

I did something slightly different. In your example I would change the mapping file so that the getter and setter on the property that is mapped to the database to be private and lowercase just as dmusial showed. Then I created a property that was NOT mapped in the edmx file as show here (Note: While I usually would make member fields _jobTitle, I'm using the code generation and starting with an _ is not allowed in EF 5.x).

    ///<summary>
    /// Private member mapped in .edmx file
    /// Something like:
    /// <Property Name="jobTitle" Type="String" MaxLength="Max" FixedLength="false" 
    /// a:SetterAccess="Private" a:GetterAccess="Private" 
    /// xmlns:a="http://schemas.microsoft.com/ado/2006/04/codegeneration" />
    ///</summary>
    private string jobTitle { get; set; }
    ///<summary>
    /// Publicly visible property that now contains your logic.
    ///</summary>
    public string JobTitle
    {
        get { return jobTitle; }
        set { jobTitle = SetProperCase(value); }
    }

Now when SaveChanges is called it should save the jobTitle property into the column to which it is mapped in your edmx file.

Upvotes: 2

dknaack
dknaack

Reputation: 60556

Description

You can ignore the propertie using the ModelBuilder and .Ignore(...)

.Ignore(...) Excludes a property from the model so that it will not be mapped to the database.

Sample

public class MyDbContext : DbContext
{
    public DbSet<Contact> Contact { get; set; }
    // other DbSet's

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Contact>().Ignore(x => x.JobTitle);
    }
}

More Information

Upvotes: 3

dmusial
dmusial

Reputation: 1564

You can write the logic in there if you want, just transform the property into a non-automatic one and perform the checks as you would do with a normal property.

 private string jobTitle;
 public string JobTitle
 {
      get { return jobTitle; }
      set
      {
           // do your fancy stuff or just jobTitle = value
      }
 }

Remember that if you change the value from db inside your setter it will probably be saved that way later on after you perform SaveChanges() on the context.

Upvotes: 17

Related Questions