Entity Framework, SQLite and Lazy loading

Hi, I developed a C # Budget application using SQL Compact and EF4, I created an EF model using the VS2010 object data model template. Everything works very well. However, I am considering developing an application for the iPhone to support cash transactions and thought it would be better to have database support on both platforms. After creating SQLite DB and creating a new model, I ran into a problem while trying to access the reference data through the navigation properties in my model. I get a NullReferenceException when trying to display a property of a reference table.

When using the following code, I get an exception in 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.
}

The strange thing is that the exception does not occur in this example. I'm not sure what's 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.
}

, , , .Include( "BankAccountTypes" ), , , , , , .

EDIT: , , , . . . SQL Compact SQLite Guid. Guid "7cee3e1c-7a2b-462d-8c3d-82dd6ae62fb4", x'7cee3e1c7a2b462d8c3d82dd6ae62fb4 '

, , , :)

.

+3
2

:

var accoutTypes = budget.BankAccountTypes;

, (EF , ).

, ( a ). , . , ( budget.ContextOptions.LazyLoadingEnabled).

+2

, BankAccountType BudgetEntities.

0

All Articles