RTUR.NET

.NET and Open Source: better together

NAVIGATION - SEARCH

Entity Framework 7 Getting Started

If you are new to Entity Framework, especially to all new EF7, best way to start is to look at this code from sample application. It shows all you need to know to get rolling: connecting to database, creating a table, inserting and selecting data all in one simple console app. You can run it in the new shiny Visual Studio 2015 with break points on any line that you want to verify and literally walk step by step understanding the process. Which is:

  1. Create entity class to represent the table (Blog). 
  2. Create class that inherits from DbContext.
  3. Declare your entity in the DbContext.
  4. Register provider (UseSqlServer) and connection string as context options.
At this point your "back-end" is set and ready to go. To use it from the client:
  1. Use DbContext instance first making sure it creates database if not exists.
  2. Create entity object and add it to context.
  3. Save your changes with SaveChangesAsync.
  4. And finally query database with LINQ as usual.
You don't have to use async and without it code would be even simpler, but this is a good practice and doesn't add much complexity after all.

public class Program
{
  public static async Task Main()
  {
    using (var db = new MyContext())
    {
      await db.Database.EnsureCreatedAsync();
    }

    using (var db = new MyContext())
    {
      var nextId = db.Blogs.Any() ? db.Blogs.Max(b => b.BlogId) + 1 : 1;
	  
      await db.AddAsync(new Blog { 
	BlogId = nextId, 
	Name = "Another Blog", 
	Url = "http://example.com" 
      });
      
      await db.SaveChangesAsync();
	   
      var blogs = db.Blogs.OrderBy(b => b.Name);
      foreach (var item in blogs)
      {
        Console.WriteLine(item.Name);
      }
    }
  }
}

public class MyContext : DbContext
{
  public DbSet<Blog> Blogs { get; set; }

  protected override void OnConfiguring(DbContextOptions builder)
  {
    builder.UseSqlServer(@"Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;");
  }
}

public class Blog
{
  public int BlogId { get; set; }
  public string Name { get; set; }
  public string Url { get; set; }
}

There are lots of other neat code in that sample app, so you might spend some time looking around. Next ASP.NET is still in the works, with very few documentation and tutorials available. In this situation looking at the code developers use themselves is very helpful.

Bootstrap List Pager

Bootstrap provides nice lists to display all kind of items your website might need. It is very useful, and because it meant to show just few items it is not paged. Problem is, sometimes I do need it paged. For most cases, it can be just a few rows, but for some it can grow in few dozens. Not hundreds, that would mean you need a grid, but still having dozens rows would make UI look really ugly. For these cases, I would want paged list kind of like one on the right in this picture. More...

Dynamic compilation in ASP.NET

We all know about magic App_Code folder. Just drop class file in there and it will become a part of the web application. This is fine for scripts like PHP or "classic" ASP (VB script), but C# is strongly typed compiled language. How App_Code works? As any magic, mostly smoke and mirrors. Behind the scene, ASP.NET will create App_Code.dll and merge it with main application assembly at run time. This simple trick gives us best of both worlds - dynamism of scripting languages (ok, to the point) and all the good stuff coming with strong typing and compilation (whatever they are). More...

Serializing XMLDocument to binary format

Why in the world would you want to do that? Well, for one, you might want to use it in Remoting. Second possibility is when you have different objects that you want to treat the same way, and XML Document is one of them. Whatever your situation is, XML Document is not serializable as is. You'll have to provide your own implementation - create class that inherits from XMLDocument and implements ISerializable interface: More...

Sorted GridView

Did it ever happened to you? You have a problem and, after research and brainstorming, finally come up with this nice elegant solution. Two month (and hundred problems) later, you are staring at the screen with basically the same task in front of you. And trying to figure out - how the hell did you do it in the first place? Yes, I'm talking knowledge base - not an original idea and lots of people use their blogs exactly for this reason. I'm joining the crowd and will publish here code snippets under "code snippets" category. Here is the first one. More...