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;
        }

   .....

 

posted @ 2016-11-24 21:54  jackchain  阅读(830)  评论(0编辑  收藏  举报