zoukankan      html  css  js  c++  java
  • ASP.NET Core + EF6

    微软推出了全新的 .Net Core,于是我就想着在目前接手的项目中使用这项新技术。但是因为预算的原因,我们所用的数据库是 MySql 。但是最新的 EF Core 并不支持 MySql ,所以目前我还是使用的 EF6 。然而微软官网并没有这方面相关的例子,所以今天折腾了一整天,终于把 EF 6 成功配置在了 .Net Core 项目中。

    准备

    • VS 2015 中新建一个 ASP.NET Core 项目(由于EF 6 不支持 CoreCLR,所以使用 .Net Framework)
    • 一个根据 Code First 定义的 DbContext 类以及相应的 Entity 类(由于目前的 VS 2015 在 ASP.NET Core 项目中不能直接由数据库生成 Code First 代码,所以我们需要自己手写 Code First 类)

    使用

    现在来看看我定义的 DbContext

    public partial class ParkDbContext : DbContext
    	{
    		public ParkDbContext() : base("Data Source=DESKTOP-M31J37E;Initial Catalog=moneyManager;User ID=zeeko;Password=*******")
    		{
    		}
    
    		public virtual DbSet<LoginInfo> LoginInfo { get; set; }
    		public virtual DbSet<IncomeAndSpending> IncomeAndSpending { get; set; }
    
    		protected override void OnModelCreating(DbModelBuilder modelBuilder)
    		{
    			modelBuilder.Entity<IncomeAndSpending>()
    				.Property(e => e.Type)
    				.IsUnicode(true);
    		}
    	}
    

    出于简单,我们就直接在Controller中使用

    public class TestController : Controller
    	{
    		private ParkDbContext dbContext=new ParkDbContext();
    
    		public string Index()
    		{
    			var result = (
    						 from item in dbContext.IncomeAndSpending
    						 select item
    						 ).FirstOrDefault();
    			return result.Type;
    		}
    	}
    

    这样的话,我们就完成了连接!

    重构

    上面的做法其实是不好的,如果后期我们需要更换数据库,我们需要修改DbContext类,这样做是不对的。
    而且在之前的 MVC 4 的项目中,我们是在 web.config 文件中定义的连接字符串,所以我们在 .Net Core 中的如何使用类似的方式来做呢?

    重构

    首先,我们需要将DbContext的构造函数改造一下

    public ParkDbContext(string connectionString) : base(connectionString)
    		{
    		}
    

    然后我们在控制器中通过构造函数注入

    public TestController(ParkDbContext context)
    		{
    			dbContext = context;
    		}
    

    然后我们在 appsettings.json 文件中定义一下连接字符串

    {
      "ConnectionStrings": {
        "DefaultConnection": "Data Source=DESKTOP-M31J37E;Initial Catalog=moneyManager;User ID=zeeko;Password=windows10zt"
      },
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Debug",
          "System": "Information",
          "Microsoft": "Information"
        }
      }
    }
    

    然后在 Startup.cs 文件中的 ConfigureServices 方法中注册一下

    public void ConfigureServices(IServiceCollection services)
    		{
    			// Add framework services.
    			services.AddMvc();
    			services.AddScoped(sp => new ParkDbContext(Configuration["ConnectionStrings:DefaultConnection"]));
    			// Add application services.
    		}
    

    至此,我们就可以在配置文件中修改连接字符串了!

    后续

    接下来,我会试着连接到 MySql


    参考文章:
    重构 ASP.NET 5/EF6 项目和依赖关系注入

  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/JacZhu/p/5631075.html
Copyright © 2011-2022 走看看