.NET and Open Source: better together

RTur.net

  • Join Us on Facebook!
  • Follow Us on Twitter!
  • LinkedIn
  • Subcribe to Our RSS Feed

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.

Comments (5) -

  • mariaandreson

    11/17/2014 7:52:24 PM |

    Entity Framework 7 post is very useful for newbies, i have learnt many new things to connecting to database, creating tables using Framework 7. Really helpful post.  

  • ถุงยาง

    3/13/2015 10:10:32 PM |

    Thanks

  • ranjanbd

    4/22/2015 11:45:10 PM |

    thanx for shearing this post.  it is very helpful for us.

  • Rasel Rana // Bangla Choti Golpo

    4/25/2015 8:13:46 PM |

    These are very nice post . I have read this post and i learn a more information Framework 7. It really helpful post for me.

  • Akib Rahat

    4/30/2015 7:10:51 AM |

    this is a very important post. thanks for sere it. i have read this post about Framework 7. i have learn many new things. it is really good post. thank u again.

Comments are closed