Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #22952
StingyJack opened this issue Jun 9, 2019 · 6 comments
Open
Tracked by #22952

Comments

@StingyJack
Copy link

StingyJack commented Jun 9, 2019

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?

@StingyJack StingyJack changed the title What is the opposite of [NotMapped]? What is the opposite of [NotMapped] (as an opt-out mechanism)? Jun 9, 2019
@AndriySvyryd
Copy link
Member

This would be possible by removing some of the default conventions, tracked in #214

Linking the docs issue: https://github.com/aspnet/EntityFramework.Docs/issues/1516

@StingyJack
Copy link
Author

@AndriySvyryd - im not sure I understand. I'm looking for something I can decorate a class and members with (either by fluent or preferably, attributes ) to instruct EF on the classes and properties it should be using.

Is there really nothing available in EF6 or EFCore for this?

@AndriySvyryd
Copy link
Member

@StingyJack There's no specific switch for this.
For EF6 you can use this workaround: https://stackoverflow.com/a/31068050/1171992
For EF Core you can do something similar:

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
    var entityTypeBuilder = modelBuilder.Entity(entityType.ClrType);
    foreach (var navigation in entityType.GetNavigations())
    {
        entityTypeBuilder.Ignore(navigation.Name);
    }

    foreach (var property in entityType.GetProperties())
    {
        entityTypeBuilder.Ignore(property.Name);
    }
}

@StingyJack
Copy link
Author

That seems like a lot to have to do when attributes like [Table] and [Column] are available. Can Opt In attributes be added? Being able to opt-in classes and properties would resolve at least one of my long standing complaints/adoption blockers regarding EF.

@ajcvickers ajcvickers changed the title What is the opposite of [NotMapped] (as an opt-out mechanism)? Add sugar to allow opt-in mapping for properties/relationships/etc. Jun 14, 2019
@ajcvickers
Copy link
Member

Putting this on the backlog to consider adding sugar that would remove some conventions and set EF Core to use opt-in mapping. Exactly what this means remains to be determined. For example, does mapping a navigation property imply FK convention mapping or not?

@ajcvickers ajcvickers added this to the Backlog milestone Jun 14, 2019
@StingyJack
Copy link
Author

StingyJack commented Jun 16, 2019

does mapping a navigation property imply FK convention mapping or not?

It would if you decorated it with something like [ForeignKey(typeof(ParentTable), "columnName")].

EF having this would mean it could play well with others instead of being the DAL bully or separatist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants