代码之家  ›  专栏  ›  技术社区  ›  serge

ASP中的表存储与DBContext。净核心

  •  3
  • serge  · 技术社区  · 7 年前

    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个步骤:

    1. 配置服务(通过Startup.cs中的ConfigureServices(IServiceCollection)),
    2. 将“IAzureTable context”传递给控制器的构造函数?。

    我是一个完全的新手,一个步骤的代码示例将不胜感激。

    Azure DBContext (如有需要)?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Brando Zhang    7 年前

    据此 article (EF的路线图),作为EF的dbcontext数据库的azure表存储现在不支持。它具有高优先级功能,将在未来发布。

    您可以看到“高优先级功能”包含azure表提供程序,如下所示。

    High priority features
    
    Providers
    
    Azure Table Storage
    Redis
    Other non-relational databases  
    

    如果您想在net core中使用azure表存储,我建议您可以使用azure storage SDK(从Nuget包安装)并编写自己的逻辑来CRUD数据。