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

我的未来我决定 提交于 2019-12-08 14:58:36

问题


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.


回答1:


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.




回答2:


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

  • EF Features: Fluent API Samples



回答3:


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.



来源:https://stackoverflow.com/questions/8990319/have-a-custom-setter-on-property-for-ef-code-first-model-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!