为了连接到ASP.NET Core Application中的数据库,我们创建了一个DbContext
namespace MyProject.Models
{
public class MyProjectContext : DbContext
{
public MyProjectContext (DbContextOptions<MyProjectContext> options)
: base(options){ }
public DbSet<MyProject.Models.Record> Record { get; set; }
}
}
在Startup.cs中我们做
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")));
}
最后在控制器中我们有
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());
}
好的,这一切都来自VS脚手架......
=============================================== ==============
我现在要处理 AzureTables ,所以我做了一个测试控制器
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();
}
问题
如何以与SQL上下文相同的方式集成Azure表?我的意思是,Azure表具有相同的3个步骤:
我是完整的新手,这些步骤的代码示例将不胜感激。
例如,如何创建 Azure DBContext (如果需要这样的话)?
答案 0 :(得分:1)
根据此article(EF&#39;路线图),作为EF的dbcontext数据库的azure表存储现在不支持。它具有高优先级功能,将在未来发布。
所以我们现在无法在.net核心中将表存储用作EF的dbcontext。
您可以看到&#34;高优先级功能&#34;包含azure表提供程序,如下所示。
High priority features
Providers
Azure Table Storage
Redis
Other non-relational databases
如果你想在net core中使用azure表存储,我建议你可以使用azure storage SDK(从Nuget包安装)并将你自己的逻辑写入CRUD数据。