zoukankan      html  css  js  c++  java
  • Asp.net Core中使用Entity Framework Core CodeFirst

    1.安装对应的包

    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",

    project.json tools节点更新
    "tools": {
            "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final",
            "Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
            "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final"
        },
    appsettiongs.json中配置连接串
    "ConnectionStrings": {
            "DefaultDB": "Persist Security Info=True;Data Source=。;User ID=sa;Password=123;DataBase=test;"
        }
    Entity & DbContext
    public class VIP_Info
        {
            public VIP_Info()
            {
    
            }
            
            public Guid Id { get; set; }
            public string Name { get; set; }
    
            public string MobilePhone { get; set; }
            
            public string Sex { get; set; }
            
            public int Score { get; set; }
        }
    public partial class SqlServerDbContext : DbContext
        {
            //方法一
            //public SqlServerDbContext(DbContextOptions<SqlServerDbContext> options)
            //: base(options)
            //{ }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                string connectionString = (string) CacheHelper.CacheValue("sqldbConStr");
                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    var configuration = builder.Build();
                    connectionString = configuration.GetConnectionString("DefaultDB");
                    CacheHelper.CacheInsertAddMinutes("sqldbConStr", connectionString, 120);
                }
                optionsBuilder.UseSqlServer(connectionString);
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
            }
    
            public virtual DbSet<VIP_Info> VIP_Info { get; set; }
        }
    关于数据库连接串的初始化

    一种方式是直接在DBContext的OnConfiguring动态去获取appsettings.json里的链接串,另一种是采用注入的方式,在Startup.cs里配置,各有优劣;上例中采用的是每次动态获取的方式,获取的连接串用全局Cache缓存下,方便下次获取。

    Startup.cs里配置连接串
    public void ConfigureServices(IServiceCollection services)
            {
                services.AddApplicationInsightsTelemetry(Configuration);
    
                services.AddDbContext<SqlServerDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultDB")));
    
                services.AddMvc()
                .AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); })
                .AddXmlDataContractSerializerFormatters();
    
            }
    public partial class SqlServerDbContext : DbContext
        {
            //方法一
            public SqlServerDbContext(DbContextOptions<SqlServerDbContext> options)
            : base(options)
            { }
    public class VipController : BaseController
        {
            private SqlServerDbContext  _context;
            public VipController(SqlServerDbContext context){
                _context=context;
            }
    
       .....
  • 相关阅读:
    TCP传输粘包问题
    提取KIndle中每本书的笔记并单独保存
    # 可视化工具资源汇总
    抓取代理IP
    Linux 小工具学习之(1)——Wget十例[翻译]
    [转]关于矩阵的本质
    用2263份证件照图片样本测试how-old.net的人脸识别
    Python生成二维码脚本
    基于git的源代码管理模型——git flow
    使用Graphviz绘图(一)
  • 原文地址:https://www.cnblogs.com/qidian10/p/6099652.html
Copyright © 2011-2022 走看看