Database Reference
In-Depth Information
7-9. Querying and Saving Asynchronously
Problem
You need to maintain the responsiveness of your application while performing queries and persisting changes to the
database.
Solution
Suppose that you have Account and Transactions POCO entities, which you've written using the Code-First modeling
strategy, like the ones shown in Listing 7-7.
Listing 7-7. Account and Transaction POCO Entities
public class Account
{
public int AccountNumber { get; set; }
public string AccountHolder { get; set; }
public virtual ICollection<Transaction> Transactions { get; set; }
}
public class Transaction
{
public int AccountNumber { get; set; }
public int TransactionNumber { get; set; }
public DateTime TransactionDate { get; set; }
public decimal Amount { get; set; }
}
The Transaction entity is clearly a dependent entity of the Account entity, so we'll configure that relationship
by creating EntityTypeConfiguration subclasses for each entity type, as shown in Listing 7-8.
Listing 7-8. Configuring the Account and Transaction Entity Types
public class AccountTypeConfiguration : EntityTypeConfiguration<Account>
{
public AccountTypeConfiguration()
{
HasKey(a => a.AccountNumber);
Property(a => a.AccountNumber)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(a => a.Transactions)
.WithRequired();
}
}
 
Search WWH ::




Custom Search