namespace MyProject.Models
{
public class MyProjectContext : DbContext
{
public MyProjectContext (DbContextOptions<MyProjectContext> options)
: base(options){ }
public DbSet<MyProject.Models.Record> Record { get; set; }
}
}
启动时。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());
}
=============================================================
AzureTables公司
启动.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个步骤:
-
-
配置服务(通过Startup.cs中的ConfigureServices(IServiceCollection)),
-
将“IAzureTable context”传递给控制器的构造函数?。
我是一个完全的新手,一个步骤的代码示例将不胜感激。
Azure DBContext
(如有需要)?