0

I am building a website with a rather complex SQL database.
I made a Query class, which holds multiple primitive fields (which all work fine) and also multiple List<SomeType>. SomeType is also defined under the Models folder in my solution. It also holds single instances of SomeType. Then, I made a QueryDBContext class which inherits DbContext.

I have a Generate function which receives a string and creates a fully formed Query instance. If I immediately send it to my View - it works fine. But if I store it in my DB - only the primitive values are kept, and the reference types (both Lists<SomeType> and SomeType) are null.

I believe the problem is that EF doesn't support reference types - or maybe I need something special for this case?
Here's Query

public class Query
{
    [Key]
    public int id { get; set; }

    public SomeType Base { get; set; }
    public List<SomeType> Derived { get; set; }
    //more of these

    public string SmallGraphImage { get; set; }
    public string MediumGraphImage { get; set; }
    public string LargeGraphImage { get; set; }
}
public class QueriesDBContext : DbContext
{
    public DbSet<Query> Queries { get; set; }
}

Here's SomeType

public class SomeType
{
    [Key]
    public int id { get; set; }
    public string ExpandedOutForm { get; set; }
    public string ExpandedInForm { get; set; }
    //more strings
}


Note: The database works fine for primitives. For developing purposes, I made a Controller which deletes & rebuilds the database for each modification. I also tried replacing List with IList.

If EF doesn't support reference types, what should I do? Will it work on structs ? How can I store Lists inside a database entry? (Yeah, I could store it in another table with its ID - but I believe EF has a more elegant solution for this)

2
  • You can't store Lists inside a database field. A database field can hold a value but not an array of values. If you need to have a reference to an array of values from a parent items, you are basically talking about having a One to many reference. Commented Aug 31, 2012 at 13:48
  • So, how can I do a one-to-many reference? Commented Aug 31, 2012 at 13:54

2 Answers 2

3

I believe the problem is that EF doesn't support reference types

The Entity Framework does support reference types. With the default configuration those will be mapped to a seperate database table. In your case, a Query_id column will be added to the table 'SomeTypes'. The Entity Framework will use those to create SQL Joins when loading data.

The reason why your properties are showing as NULL has nothing to do with this. It has to do with how the Entity Framework loads data. By default, EF will not load a whole object graph from the start. This would mean that if your SomeType references another class which references another... and you would load the whole database in one call.

You have two options:

  • Explicitly loading the references. You tell the Entity Framework which properties you are going to use in advance and it will load them in one call from the database.

    Query c = ctx.Queries .Include(x => x.Derived) .Include(x => x.Base).First();

Here you use the Include method to tell the Entity Framework to load both your Derived collection as your Base property.

  • Use lazy loading. This means that the Entity Framework will load your property when it's needed. For this to work, EF creates a proxy class that wraps your class and will execute the database calls just in time.

    For this to work, you will need to declare your public SomeType Base { get; set; } as virtual so EF can create a proxy and intercept those calls.

The only reason you where not seeing your data, is because it wasn't loaded from the database.

Sign up to request clarification or add additional context in comments.

Comments

1

I believe the problem is that EF doesn't support reference types

EF supports reference types - they're essentially mapped to other tables.

(Yeah, I could store it in another table with its ID - but I believe EF has a more elegant solution for this)

Actually that'e exactly how EF handles 1-many relationships by default. How are you intending to store those in a single table?

1 Comment

Of course EF stores then in different tables internally, my question is: how to rewrite my class structure so it will work? edit:So EF will create the appropriate DB and save the results...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.