Skip to content

Add sugar to allow opt-in mapping for properties/relationships/etc. #16009

@StingyJack

Description

@StingyJack

If I have a class that is to be used with EF as a complete and correct table mapping. This class is generated from some description elsewhere.

public partial class Car
{
  public int Id {get; set;}
  public string Name {get; set;}
  public string MfgYear {get; set;} 
  etc...
}

This class is intended to be extended with some additional properties that do not require persistence and a few methods.

public partial class Car
{
  public int MaxSpeed {get; private set;}
  public void RecalculateMaxSpeed(int accelerationTime, int accelerationRate)
  {
    //this math is for example purposes only and is not intended to be complete or correct
    MaxSpeed = accelerationTime * accelerationRate; 
  }
}

When I try to save an instance of Car, EF errors about MaxSpeed not being mapped. The advice most commonly given is to either add [NotMapped] to MaxSpeed, or to create a second Car class and add the MaxSpeed property to that and remap the EF object to the new Car on Gets and back to the EF object on Saves. The remapping is not preferable and is not an option in the scope of this post.

Adding [NotMapped] to every field not part of the entity is also ripe with chances for bugs to happen.

Is there any way to "Opt In" an object and specific properties so that EF knows which are eligible for its scope?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions