Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF4 Unable to cast concrete type to interface

Using Entity Framework LINQ, I'd like to return a parent class for every parent table and populate a property, whose type is an interface, on the parent class with one of multiple concrete implementations of an interface. Which concrete implementation should be determined at query time based on the value of a field in the parent table.

In a very simplified example, I have 3 tables and 3 corresponding POCOs.

Simple tables for the sake of example

Three Tables: Master and two children

Simple classes for the sake of example

internal interface IConfiguration
{
}

internal class ConfigurationContainer
{
    public IConfiguration Config { get; set; }
}

internal class ConfigurationSouth : IConfiguration
{
}

internal class ConfigurationNorth : IConfiguration
{
}

Unfortunately, I loop through all the parent results and determine which subquery to use while inside the loop. Something like this block.

foreach (var configMaster in db.ConfigMasters.ToList())
{
    var configContainer = new ConfigurationContainer();
    if (configMaster.IsNorth)
        configContainer.Config = (from x in db.ConfigNorths
                          select new ConfigurationNorth())
                                      .FirstOrDefault();
    else
        configContainer.Config = (from x in db.ConfigSouths
                          select new ConfigurationSouth())
                                      .FirstOrDefault();
}

Looping through each of the parent records to perform a subquery is less than optimal. I'd really like the EF LINQ to perform the query in one trip to the database as well as do the projections into my POCOs.

I came up with this LINQ to query the database in one trip and return the projected objects such that the Config property would be populated by one of the two subqueries at querytime. Though it compiles, it throws an exception at runtime.

using (var db = new Entities())
{
    var qry = from cfgMaster in db.ConfigMasters
                let configNorth = (from x in db.ConfigNorths
                                    select new ConfigurationNorth())
                    .FirstOrDefault()
                let configSouth = (from x in db.ConfigSouths
                                    select new ConfigurationSouth())
                    .FirstOrDefault()
                select new ConfigurationContainer()
                    {
                        Config = cfgMaster.IsNorth ? configNorth : (IConfiguration) configSouth
                    };

    var results = qry.ToList();
}

Exception

Unhandled Exception: System.NotSupportedException: Unable to cast the type 'EFTest.ConfigurationNorth' to type 'EFTest.IConfiguration'. LINQ to Entities only supports casting Entity Data Model primitive types.

like image 297
Nick VanderPyle Avatar asked Nov 13 '22 21:11

Nick VanderPyle


1 Answers

Entity Framework does not (yet?) support the use of interfaces this way, basically because the interface is not (and can not be) registered with the model. What you want is only possible when you let both classes derive from a base class that is part of the model too.

like image 127
Gert Arnold Avatar answered Nov 15 '22 12:11

Gert Arnold