none
EF6 Accessing SubType from Junction Table RRS feed

  • Question

  • Hello!

    I have the following table

    CREATE TABLE [dbo].[UserProducts](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [UserId] [int] NOT NULL,
    [ProductId] [int] NOT NULL,
    [DateUpdated] [datetime] NOT NULL,
    [ViewCount] [int] NOT NULL
    )

    which is a junction table between users and products. UserId and ProductId are foreign keys.

    Its class representation is as follows:

     public partial class UserProduct
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public int ProductId { get; set; }
        public System.DateTime DateUpdated { get; set; }
        public int ViewCount { get; set; }
    
        public virtual Product Product { get; set; }
        public virtual User User { get; set; }
    }

    I have a query which selects a user and their associated products as so:

    return this.context.UserProducts
                    .Where(p => p.UserId == uId && p.Product.PublisherId == pId)
                    .Where(p => !p.Product.SetOffline)
                    .OrderByDescending(p => p.DateUpdated)
                    .Take(10)
                    .ToList();

    which returns a list of UserProducts.

    I then loop through this list and return some product fields such as: @Model.UserProduct.Product.ProductName

    Now my issue. A Product consists of the Superclass "Product" and several subclasses such as ProductVehicle or ProductTyre. How can I access one of these subclasses from the above query? Currently in the result set I can't access subtypes properties (e.g., @Model.UserProduct.Product.Make) and only see the parent class "Product" properties. Note the result set must return a list of UserProducts.

    here is an example of a subtype:

     public partial class ProductsVehicle : Product
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public Nullable<short> Year { get; set; }
    }
    Monday, August 18, 2014 12:44 AM

Answers

  • Thank you for the feedback. I didn't need to implement your recommended change. In the end I could access the subtype via a cast as so:

    var vehicle = (ProductsVehicle)@Model.UserProducts[i].Product;

    which allowed me to access vehicle.Make

    Tuesday, August 19, 2014 12:59 PM

All replies

  • Hello matt_ddc,

    From your description, it seems that you create a custom many to many relationship model using code fisrt.

    From the current UserProduct entity, we need to also add the sub type to the junction table as:

    public class UserProduct
    
        {
    
            public int Id { get; set; }
    
            public int UserId { get; set; }
    
            public int ProductId { get; set; }
    
            public System.DateTime DateUpdated { get; set; }
    
            public int ViewCount { get; set; }
    
    
            public virtual Product Product { get; set; }
    
            public virtual User User { get; set; }
    
            public virtual ProductsVehicle ProductsVehicle { get; set; }
    
        }

    Then we could reach these sub properties.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, August 19, 2014 4:30 AM
    Moderator
  • Thank you for the feedback. I didn't need to implement your recommended change. In the end I could access the subtype via a cast as so:

    var vehicle = (ProductsVehicle)@Model.UserProducts[i].Product;

    which allowed me to access vehicle.Make

    Tuesday, August 19, 2014 12:59 PM