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)
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
NULLhas 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 yourSomeTypereferences 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
Includemethod to tell the Entity Framework to load both yourDerivedcollection as yourBaseproperty.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.