You are struggling with modelliing inheritance. Student and Teacher both are Persons. There are several ways to store inherited classes in a relational database, and entity framework supports these.

The approach that probably helps you best is called Table per Concrete Type (TPC), which means that each type has its own table in the database, while EF takes care of storing the subtypes in the right table. The link also refers the two other commmon approaches (TPH and TPT). TPT may also be appropriate, if your subclasses have relatively much in common.

So you would have

abstract class Person { PersonId, ... }

class Student : Person { Grade, ... }

class Teacher: Person { PrimarySubject, ... }


If you want Students you do

context.Persons.OfType&lt;Student&gt;().Where(....


which gives you the QueriedResult result as intended.

Note that Person is abstract. If you want it to be concrete, you better make an abstract PersonBase class (or the like) and derive Person from it, just as Student and Teacher.

LINQ Select New to create one table from super-type sub-type relationship
I'm having trouble when rendering a text box using @Html.TextBoxFor with a custom type I've created. My custom type looks like this:

public class Encrypted&lt;T&gt;
{
    private readonly Lazy&lt;T&gt; _decrypted;
    private readonly Lazy&lt;string&gt; _encrypted;

    public static implicit operator Encrypted&lt;T&gt;(T value)
    {
        return new Encrypted&lt;T&gt;(value);
    }

    public static implicit operator string(Encrypted&lt;T&gt; value)
    {
        return value._encrypted.Value;
    }

    ...
}


Then on my model, I have:

public class ExampleModel
{
    public Encrypted&lt;string&gt; Name { get; set; }
}


If I manually populate the value in my controller action:

public ActionResult Index()
{
    var model = new ExampleModel
    {
        Name = "Example Name";
    };
    return View(model);
}


Then on my view I have the standard @Html.TextBoxFor(m =&gt; m.Name). However, when that renders, the value of my text box gets set to: Services.Encrypted`1[System.String]`

Presumably this is because I'm using a custom type and the compiler doesn't know how to convert my type to a string value.

I've tried using a custom TypeConverter:

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
    return destinationType == typeof(string);
}

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
    if (destinationType == typeof(string))
    {
        var encrypted = value as IEncrypted;
        if (encrypted != null)
        {
            return encrypted.DecryptedValue();
        }
    }

    return null;
}


Then on my Encrypted model I added:

[TypeConverter(typeof(EncryptedTypeConveter))]


However it doesn't seem to be using the custom TypeConverter. Does anyone know how I can resolve this?
