EFCore的使用

1.安装nuget包,

Install-package Microsoft.EntityFrameworkCore
Install-package Microsoft.EntityFrameworkCore.SqlServer

Install-package Microsoft.EntityFrameworkCore.Tools

2.创建尸体上下文类

  public class TestContext : DbContext
  {
    public TestContext()
    {

    }
    /// <summary>
    /// constructor for Coding first
    /// </summary>
    /// <param name="options"></param>
    public TestContext(DbContextOptions<TestContext> options) : base(options) { 
    
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
      //optionsBuilder.UseSqlServer(@"Server=.;Database=Blogging;Integrated Security=True;User ID=tom;Password=MS410882wangcong");
      optionsBuilder.UseSqlServer(@"Server=.;Database=dbnamep;User ID=t;Password=;");
    }
    public DbSet<LocationInfo> locationInfos { get; set; }
    public DbSet<Car> cars { get; set; }
  }

  /// <summary>
  /// location information
  /// </summary>
  [Table("LocationInfo")]
  public class LocationInfo
  {
    [Key]
    public int id { get; set; }
    //[Key]
    //public int id { get; set; }
    /// <summary>
    /// name
    /// </summary>
    public string name { get; set; }

    /// <summary>
    /// address
    /// </summary>
    public string address { get; set; }
  }
  [Table("Car")]
  public class Car
  {
    [Key]
    public int id { get; set; }
    public string carName { get; set; }
  }

3.在action中使用

    [HttpPost]
    public string Post(object user) {
      User userModel = JsonConvert.DeserializeObject<User>(user.ToString());
      try {
        using (var content = new TestContext())
        {
          var log = logging;
          var a = content.cars;
          content.cars.Add(new Car() { carName = userModel.firstName });
          content.SaveChanges();
        }
      }
      catch (Exception e) {
        Console.WriteLine(e.Message);
      }
      return $"post success:{userModel.firstName}";
    }

4.Coding first,根据代码生成数据库和数据库的表

注意,一定要安装

Microsoft.EntityFrameworkCore.Tools
4.1在startup类中声明
      #region  EF Coding first
      services.Configure<CookiePolicyOptions>(options =>
      {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
      });

      var connectionString = Configuration.GetConnectionString("DefaultConnection");
      services.AddDbContext<TestContext>(options =>
      options.UseSqlServer(connectionString));

      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

      #endregion

4.2实体上下文添加构造函数

    /// <summary>
    /// constructor for Coding first
    /// </summary>
    /// <param name="options"></param>
    public TestContext(DbContextOptions<TestContext> options) : base(options) { 
    
    }

4.3 Tools=>Nuget package manager => package manager console

先输入 Add-Migration FirstMigration,再输入Update-Database。迁移成功后,会创建数据库,以及会在项目中生成一个Migrations文件夹,里面时迁移记录。

EFCore的使用

 

 



 

EFCore的使用

上一篇:jenkins active choice plugin


下一篇:centos 8 gitlab 重置管理员的密码