• EF Core 搭建单侧环境


    有时候想搭个环境做测试, 又记不住那些 command, 官方教程又啰嗦. git clone 模板又不太好管理, 索性记入在这里吧.

    创建项目

    dotnet new webapp -o SimpleTestEFCore

    Install NuGet

    dotnet add package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore

    dotnet add package Microsoft.EntityFrameworkCore.Design

    dotnet add package Microsoft.EntityFrameworkCore.SqlServer

    Files

    Product.cs

    namespace AzureGetStarted.Entity
    {
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; } = "";
        }
    }

    ApplicationDbContext.cs

    using Microsoft.EntityFrameworkCore;
    
    namespace AzureGetStarted.Entity
    {
        public class ApplicationDbContext : DbContext
        {
            public ApplicationDbContext(
               DbContextOptions<ApplicationDbContext> options
            ) : base(options)
            {
            }
    
            public DbSet<Product> Products => Set<Product>();
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Product>().ToTable("Product");
                modelBuilder.Entity<Product>().Property(e => e.Name).HasMaxLength(256);
            }
        }
    }

    appsettings.json

    "ConnectionStrings": {
      "ApplicationDbContext": "Server=192.168.1.152;Database=AzureGetStarted;Trusted_Connection=True;MultipleActiveResultSets=true"
    }

    Startup.cs > ConfigureServices

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

    cmd

    dotnet ef migrations add init
    dotnet ef database update

    Index.cshtml

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.Extensions.Logging;
    using AzureGetStarted.Entity;
    using Microsoft.EntityFrameworkCore;
    
    namespace AzureGetStarted.Pages
    {
        public class IndexModel : PageModel
        {
            private readonly ILogger<IndexModel> _logger;
            private readonly ApplicationDbContext _db;
    
            public IndexModel(
                ILogger<IndexModel> logger,
                ApplicationDbContext Db
            )
            {
                _logger = logger;
                _db = Db;
            }
    
            public async Task OnGetAsync()
            {
                var products = await _db.Products.ToListAsync();
                _db.Products.Add(new Product { Name = "Product1" });
                await _db.SaveChangesAsync();
            }
        }
    }
  • 相关阅读:
    用户界面线程(含有消息泵的线程)主线程与用户界面线程的通信
    poj 2151 Check the difficulty of problems 概率dp
    Codeforces 148D Bag of mice 概率dp
    [置顶] UVa在线比赛单题汇总DP专题
    UVa 10405 Longest Common Subsequence 最长公共子序列模板
    UVa 12018 Juice Extractor 切水果dp暂时存疑
    UVa 674 Coin Change 背包dp
    hdu 3853 LOOPS 圆环之理 概率dp
    UVa 111 History Grading 最长递增子序列 LIS
    UVa在线比赛单题汇总DP专题
  • 原文地址:https://www.cnblogs.com/keatkeat/p/15047395.html
Copyright © 2020-2023  润新知