1

How do I map two class members to one column in Entity Framework database? Is it possible? I tried the following code and received errors

        modelBuilder.Entity<Product>(entity =>
        {

            entity.Property(e => e.Id)
            .HasColumnName("ProductId");

            entity.Property(e => e.ProductId)
                .HasColumnName("ProductId");

Error Run Time:

'Product.Id' and 'Product.ProductId' are both mapped to column 'ProductId' in 'Product' but are configured with different value generation strategies.'

We are trying to resolve this issue, otherwise the generic repository will have to use Expression Builders

Net Core: Generic Repository Primary Id Key Performance in Entity Framework

Their top solution was not working.

var idName = _context.Model.FindEntityType(typeof(TEntity))
        .FindPrimaryKey().Properties.Single().Name;
  • 1
    Sounds like one (e.g.: `Id`) should be decorated with `[NotMapped]` and have a getter/setter that mirrors the other property. – AlwaysLearning Sep 25 '19 at 00:19
  • hi @AlwaysLearning asked discussed into this link, get;set; causes performance issues https://stackoverflow.com/questions/57386900/net-core-generic-repository-primary-id-key-performance-in-entity-framework –  Sep 25 '19 at 02:38
  • can you please elaborate in the question why you need 2 properties mapped to a single column, what do you try to achieve? Just to make sure there is no [XY problem](https://en.wikipedia.org/wiki/XY_problem). – fenixil Sep 25 '19 at 03:24
  • well I am reviewing previous code, some people utilize Id, others ProductId, so need to work in both situations if possible –  Sep 25 '19 at 03:25
  • Have you considered changing all usages of `Id` to `ProductId`? Having two separate properties like that gives EF a hard choice when both of them get updated on the same instance: Which one is correct? Which one should be persisted? Take that possibility away. – AlwaysLearning Sep 25 '19 at 04:22
  • hi @AlwaysLearning well our existing code base, everyone is using Id and ProductId, so trying to see if I can use both ways, thanks- –  Sep 25 '19 at 05:08
  • That's my point. If you change all instances of `Id` to `ProductId` then your team will have no choice but to standardize on `ProductId` and you will have removed a potential source of bugs and performance issues. – AlwaysLearning Sep 25 '19 at 08:45

1 Answers1

1

Generic repositories are an anti-pattern with Entity Framework. They're honestly not worth the hassle.

However, to map a ProductID column in the entity:

entity.Property(e => e.Id)
    .HasColumnName("ProductId");
entity.Ignore(e => e.ProductId);

The catch here is that when you're writing your Linq expressions that would go through EF to SQL you need to avoid using any ignored properties.

Alternatively I'd suggest removing the ProductId field in the entity and simply relabel the field in your view models & DTOs that front-end code and any serializer uses, setting up Automapper to translate the ID column.

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • 1
    hi Stevepy, question is how to map Two Class members to a Column name, (not 1 class member, and ignore the other one); I also want to be able to use both class members, the last part 'relabel front end code' is the problem we are trying to avoid :), I was placed into the program later, trying to resolve this, thanks however ! –  Sep 25 '19 at 02:37
  • 2
    The answer to that question is simple. "You can't". The entity must map one property to one field. My point is that if the whole reason you need an "Id" column while the table has "ProductId" was to implement a Generic Repository pattern, then the solution would be to ditch the Generic Repository. For read-only you could bind the entity to a View that selects both the ProductId and ProductId AS Id, but seriously what would be the point in any of that? – Steve Py Sep 25 '19 at 03:18