Database Reference
In-Depth Information
public class TransactionTypeConfiguration : EntityTypeConfiguration<Transaction>
{
public TransactionTypeConfiguration()
{
HasKey(t => new {t.AccountNumber, t.TransactionNumber});
Property(t => t.TransactionNumber)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
Finally, in Listing 7-9, we set up the DbContext subclass and implement an override of the OnModelCreating
method in which we add the entity configurations to the model builder's Configurations collection.
Listing 7-9. Creating the DbContext Subclass
public class EF6RecipesContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
public DbSet<Transaction> Transactions { get; set; }
public EF6RecipesContext() : base("name=EF6CodeFirstRecipesContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new AccountTypeConfiguration());
modelBuilder.Configurations.Add(new TransactionTypeConfiguration());
}
}
In order to query and save asynchronously, we will use the ForEachAsync() LINQ-to-Entities method and the
SaveChangesAsync() DbContext method respectively. The code in Listing 7-10 demonstrates the usage of each
method.
Listing 7-10. Querying and Saving Entities Asynchronously
static void Main(string[] args)
{
RunExample().Wait();
Console.ReadKey(true);
}
static async Task RunExample()
{
using (var context = new EF6RecipesContext())
{
var account1 = new Account
 
Search WWH ::




Custom Search