serge
serge

Reputation: 15239

Table Storage vs DBContext in ASP.NET Core

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)?

Upvotes: 3

Views: 1206

Answers (1)

Brando Zhang
Brando Zhang

Reputation: 28387

According to this article(EF's roadmap), the azure table storage as EF's dbcontext database doesn't support now. It is in the high priority features and will be released in the future.

So we couldn't use the table storage as EF's dbcontext in the .net core now.

You could see the "High priority features" contains the azure table provider as below.

High priority features

Providers

Azure Table Storage
Redis
Other non-relational databases  

If you want to use azure table storage in the ,net core, I suggest you could use azure storage SDK(installed from the Nuget package) and write youe own logic to CRUD the data.

Upvotes: 1

Related Questions