Table Storage vs DBContext in ASP.NET Core

Viewed 994

In order to connect to a database in the ASP.NET Core Application we create a DbContext

namespace MyProject.Models
{
    public class MyProjectContext : DbContext
    {
        public MyProjectContext (DbContextOptions<MyProjectContext> options)
            : base(options){ }

        public DbSet<MyProject.Models.Record> Record { get; set; }
    }
}

In Startup.cs we do

public void ConfigureServices(IServiceCollection services) {
    // Adds services required for using options.
    //...
    // Add framework services.
    services.AddMvc();

    services.AddDbContext<MyProjectContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyProjectContext")));
}

And Finally in the Controller we have

namespace MyProject.Controllers
{
    public class RecordsController : Controller
    {
        private readonly MyProjectContext _context;

        public RecordsController(MyProjectContext context) {
            _context = context;    
        } 

        // GET: Records
        public async Task<IActionResult> Index() {
            return View(await _context.Record.ToListAsync());
        }

OK, all that is from the VS scaffolding...

=============================================================

I have now to do with AzureTables, so I did a test controller that does

Startup.cs

    public void ConfigureServices(IServiceCollection services) {
        // Adds services required for using options.
        ...
        // Register the IConfiguration instance which "ConnectionStrings" binds against.
        services.Configure<AppSecrets>(Configuration);

HelloWorldController.cs

namespace MyProject.Controllers
{
    public class HelloWorldController : Controller
    {        
        CloudTableClient cloudTableClient = null;

        public HelloWorldController(IOptions<AppSecrets> optionsAccessor) {
            string azureConnectionString = optionsAccessor.Value.MyProjectTablesConnectionString;
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(azureConnectionString);
            cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
        }    

        public async Task<string> ReadTables() {
            CloudTable table = cloudTableClient.GetTableReference("themes");

            StringBuilder response = new StringBuilder("Here is your test Table:");
            var query = new TableQuery<DescriptionEntity>() {
                SelectColumns = new List<string> { "RowKey", "Description" }
            };
            var items = await table.ExecuteQuerySegmentedAsync<DescriptionEntity>(query, null);
            foreach (DescriptionEntity item in items) {
                response.AppendLine($"Key: {item.RowKey}; Value: {item.Description}");
            }

            return response.ToString();
        } 

Question

How can I integrate the Azure Tables in the same way like the SQL Context does? I mean, having the same 3 steps for the Azure Tables:

  1. Create the Azure Tables Context,
  2. Configure Services (via ConfigureServices(IServiceCollection) in Startup.cs),
  3. Pass the "IAzureTable context" to the Controller's constructor ?.

I am complete newbie, a code example of the steps would be greatly appreciated.

How, by eg, create the Azure DBContext (if there is a need for such one)?

1 Answers
Related