简体   繁体   中英

Entity Framework - Code First - Map results to Not Mapped properties

I have created these entities Product , Order , OrderedItem in EF using Code First.

public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; }

    [NotMapped]
    public int IssuedQuantity { get; set; }

    [NotMapped]
    public int InhandQuantity { get; set; }

    public virtual ICollection<OrderedItem> OrderedItems { get; set; }
    ...
}

public class Order
{
    public int Id { get; set; }
    public string ReferenceNumber { get; set; }

    public virtual ICollection<OrderedItem> OrderedItems { get; set; }
    ...
}

public class OrderedItem
{
    public int OrderId { get; set; }
    public string ProductId { get; set; }

    [ForeignKey("OrderId")]
    public virtual Order Order { get; set; }

    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
    ...
}

Now I want to get all products by passing current user id to a stored procedure. It will then return all products along with total product quantity currently in user's hand.

The problem is that EF is not mapping SP results back to Product entity for NotMapped properties. ie all properties in product entity have values but NotMapped properties are set to NULL even when I return their values from SP.

What I want to ask is that does EF support this kind of functionality? If yes then how?

NOTE I know about Computed Properties but that will create unneccessary columns in tables and I don't want that, since these properties are calculated at run-time.

NOTE I know that I don't need to create OrderedItem entity. But I am storing some other properties in it, which are removed here for brevity.

I'm quite sure that EF does not support dynamic mapping (you could try to change the mapping metadata but is not a clean way or delete the mapping cache but then EF will be very slow). In this case the razionale is that the entity are 2 different entities because they have different data. In your case probably the best thing is to do 2 entities the ProductWithQuantities that inherits from Product.

BTW Thinking about ERPs, the model of orders/wms usually is different. Products does not contain informations about QtyOnHand or sales/buy information. Usually is another object (Inventory?) that contains this informations.

I would create a View Model of the product with all the required properties and pass that to the view instead of the Product model. Then you are not constrained by the mappings of the Product model and you do not have to use the [NotMapped] Attribute on the fields.

    [NotMapped]
    public class ProductVM 
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int IssuedQuantity { get; set; }

        public int InhandQuantity { get; set; }

        public virtual ICollection<OrderedItem> OrderedItems { get; set; }
        ...
    }

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM