Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework, SQLite and Lazy loading

Hi I had developed a C# Budget application using SQL Compact and EF4, I created the EF model through the VS2010 Entity Data Model template. It is all working very well. However I am considering developing a iPhone app to support cash transactions and thought it would be better to have the back end DB supported on both platforms. After creating the SQLite DB and creating a new model I have come across a problem when trying to access referenced data via the Navigation properties in my model. I am getting a NullReferenceException when trying to display a property of a referenced table.

When using the following code I get the exception on the last line:

BudgetEntities budget = new BudgetEntities();
var accounts = budget.BankAccounts.ToList();

foreach (BankAccount a in accounts)
{
    Console.WriteLine("Name:" + a.Description);
    Console.WriteLine("Number:" + a.AccountNumber);
    Console.WriteLine("Type:" + a.BankAccountType.AccountType); //Exception occurs here.
}

Strange thing is that the exception doesn't occur in this example. I'm not sure what is going on?

BudgetEntities budget = new BudgetEntities();
var accoutTypes = budget.BankAccountTypes;

var account = new BankAccount();
account.ID = Guid.NewGuid();
account.AccountTypeID = accoutTypes.First(t => t.AccountType.StartsWith("Credit")).ID;
account.BSB = "3434";
account.AccountNumber = "32323";
account.Description = "Test";
account.TrackingAccount = true;

budget.AddObject("BankAccounts", account);
budget.SaveChanges();
var accounts = budget.BankAccounts.ToList();

foreach (BankAccount a in accounts)
{
    Console.WriteLine("Name:" + a.Description);
    Console.WriteLine("Number:" + a.AccountNumber);
    Console.WriteLine("Type:" + a.BankAccountType.AccountType); //Exception doesn't happen.
}

This is only a simple example and I know I could fix it by adding .Include("BankAccountTypes") to the query however I have other queries that are quite complex that are creating object which include properties from referenced object with in the query and I am not quite sure how to get around this issue for them.

EDIT: After having a break between projects I have come back to this problem and I have finally resolved my problem. it had nothing to do with the code. It was with the data. I had converted a SQL Compact database to SQLite via a dump and load and had the syntax wrong for my Guid column data. I was inserting the Guid as '7cee3e1c-7a2b-462d-8c3d-82dd6ae62fb4' when it should have been x'7cee3e1c7a2b462d8c3d82dd6ae62fb4'

Hopefully the hair I pulled out working through this problem will grow back :)

Thanks everyone for your input.

like image 323
ruskie Avatar asked Dec 06 '25 15:12

ruskie


1 Answers

In second example your code snippet begins with:

var accoutTypes = budget.BankAccountTypes;

This loads all bank account types to your application and you don't need lazy loading anymore (EF will automatically recognize that these entities were already loaded and fix relations with bank accounts).

First check if your account class is dynamic proxy (just check type of a in the debugger). If it is not you made some mistake in the class definition and lazy loading will not work. Next check if lazy loading is enabled on your context instance (budget.ContextOptions.LazyLoadingEnabled property).

like image 109
Ladislav Mrnka Avatar answered Dec 08 '25 03:12

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!