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",sql

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

   .....
相關文章
相關標籤/搜索