2

I have coded a C# MVC5 Internet application, and want to know if I can use interfaces with EF6, and if not, how else I should code my classes.

Here is what I have that does work:

public class TestObjectWithObjectList
{
    [Key]
    public int Id { get; set; }
    public virtual List<TestObjectItem> items { get; set; }

    public TestObjectWithObjectList()
    {
        items = new List<TestObjectItem>();
    }
}

I have a DbSet<TestObjectWithObjectList>. When I add a TestObjectWithObjectList to the DbSet, I also add a TestObjectItem to the List<TestObjectItem>. When I retrieve the TestObjectWithObjectList, the List<TestObjectItem> has a count of 1.

Here is the same code, but with a List<ITestObjectItem>:

public class TestObjectWithInterfaceList
{
    [Key]
    public int Id { get; set; }
    public virtual List<ITestObjectItem> items { get; set; }

    public TestObjectWithInterfaceList()
    {
        items = new List<ITestObjectItem>();
    }
}

I have a DbSet<TestObjectWithInterfaceList>. When I add a TestObjectWithInterfaceList to the DbSet, I also add a TestInterfaceObjectItem (that implements the ITestObjectItem interface) to the List<ITestObjectItem>. When I retrieve the TestObjectWithInterfaceList, the List<ITestObjectItem> has a count of 0.

Is it possible to get the above code to work with a List<ITestObjectItem>? What I want to do is to be able to add many objects to the List<ITestObjectItem>, and be able to retrieve them just by getting the TestObjectWithInterfaceList object.

Thanks in advance

Simon
  • 7,991
  • 21
  • 83
  • 163

0 Answers0