Pages

Friday, May 15, 2015

EF6 code first

install-package EntityFramework
or
update-package EntityFramework




Install EF Power Tools  to visualize code first

Context class


namespace EF6Tutorial {
    public class BooksContext:DbContext {
        public BooksContext()
            : base("BooksConnection") {

        }
        public DbSet Titles {get;set;}</div> <div>         public DbSet<genre> Genres {get;set;}</div> <div>         public DbSet<author> Authors {</div> <div>             get;</div> <div>             set;</div> <div>         }</div> <div>         public DbSet<titlemetadata> TitleMetadatas {</div> <div>             get;</div> <div>             set;</div> <div>         }</div> <div> <br></div> <div> <br></div> <div>         protected override void OnModelCreating(DbModelBuilder modelBuilder) {</div> <div> <br></div> <div>             modelBuilder</div> <div>      .Entity<author>()</div> <div>      .Property(t => t.AuthorName).HasMaxLength(500).HasColumnAnnotation( IndexAnnotation.AnnotationName, </div> <div>         new IndexAnnotation(</div> <div>             new IndexAttribute("IX_AuthorName", 1)));</div> <div> <br></div> <div>             base.OnModelCreating(modelBuilder);</div> <div>         }</div> <div>      public static void Initialise(){</div> <div> <br></div> <div>          Database.SetInitializer(</div> <div>              new MigrateDatabaseToLatestVersion<BooksContext,Configuration>()</div> <div>              </div> <div>              );</div> <div>      </div> <div>      }  </div> <div> <br></div> <div>     }</div> </div> <div> <br></div> <div> ================</div> <div> <b style="background-color: rgb(255, 255, 0);">PM> enable-migrations </b></div> <div> <br></div> <div> <div>   internal class Configuration : DbMigrationsConfiguration<EF6Tutorial.BooksContext></div> <div>     {</div> <div>         public Configuration()</div> <div>         {</div> <div>           </div> <div>             AutomaticMigrationsEnabled = true;</div> <div>             AutomaticMigrationDataLossAllowed=true;</div> <div>             ContextKey = "EF6Tutorial.BooksContext";</div> <div>         }</div> <div> <br></div> <div>         protected override void Seed(EF6Tutorial.BooksContext context)</div> <div>         {</div> <div>             //  This method will be called after migrating to the latest version.</div> <div> <br></div> <div>             //  You can use the DbSet<t>.AddOrUpdate() helper extension method </div> <div>             //  to avoid creating duplicate seed data. E.g.</div> <div>             //</div> <div>             //    context.People.AddOrUpdate(</div> <div>             //      p => p.FullName,</div> <div>             //      new Person { FullName = "Andrew Peters" },</div> <div>             //      new Person { FullName = "Brice Lambson" },</div> <div>             //      new Person { FullName = "Rowan Miller" }</div> <div>             //    );</div> <div>             //</div> <div>         }</div> <div>     }</div> </div> </div>

Tuesday, May 12, 2015

async , await .NET 4.5


you still can use Task.Wait method


 The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.
The asynchronous method in which await is used must be modified by the async keyword. Such a method, defined by using the async modifier, and usually containing one or more await expressions, is referred to as an async method.


private async Task SumPageSizesAsync()
{
    // To use the HttpClient type in desktop apps, you must include a using directive and add a
    // reference for the System.Net.Http namespace.
    HttpClient client = new HttpClient();
    // . . .
    Task getContentsTask = client.GetByteArrayAsync(url);
    byte[] urlContents = await getContentsTask;

    // Equivalently, now that you see how it works, you can write the same thing in a single line.
    //byte[] urlContents = await client.GetByteArrayAsync(url);
    // . . .
}