zoukankan      html  css  js  c++  java
  • .net core使用EF

    .net Core使用EF简单版:

    一、Nuget导入EF库

    二、写实体Model (个人喜欢DBFirst,但是Core里不能直接导入库,所以手写吧)

    三、写 DBContxt

    四、验证是否成功


    一:SQLServer/MySQL导入的库

    二、实体类

     1     using System;
     2     using System.Collections.Generic;
     3     using System.ComponentModel.DataAnnotations.Schema;
     4 
     5     [Table("t_errorlog")]
     6     public partial class t_errorlog
     7     {
     8         public System.Guid Id { get; set; }
     9         public Nullable<System.Guid> UserId { get; set; } 
    10         public string Location { get; set; }
    11         public string Content { get; set; }
    12         public System.DateTime CreateTime { get; set; }
    13     }
    View Code

    三、写 DBContxt

    public class DataModelContext:DbContext
    {
      public DataModelContext() 
      {
      }
      
    
      public DataModelContext(DbContextOptions<DataModelContext> options) : base(options)
      {  
      }
    
    
      private IConfiguration _IConfiguration = null;
    
      public DataModelContext(IConfiguration configuration)
      {
        this._IConfiguration = configuration; 
      }
    
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      { 
    
        modelBuilder.Entity<t_errorlog>().ToTable("t_errorlog").HasKey("Id"); 
    
      }
    
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      { 
        //SQL Server
         optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DbConnection"));
        //MySQL
        // optionsBuilder.UseMySql(Configuration.GetConnectionString("DbConnection") ); 
      } 
       
      
      public virtual DbSet<t_errorlog> t_errorlog { get; set; } }

    四、验证是否成功

         using (Model.DataModelContext _context = new Model.DataModelContext())
         { 
             ViewBag.logCount = _context.t_errorlog.Count();
         }
  • 相关阅读:
    Delphi集合的用法
    文字倒序输出(集合)
    如果知道两点的经纬度 如何算两点之间的距离
    Arcengine 开发完后,程序打包,在目标机器上不能使用 已解决
    arcengine License部署
    设置代理
    关于GPS坐标转换的学习笔记相当头疼
    ArcEngine 相关转载
    经纬度到平面坐标的相互转换
    用ArcEngine9.3开发GIS应用程序图层符号化解决方案
  • 原文地址:https://www.cnblogs.com/ruiying/p/12673206.html
Copyright © 2011-2022 走看看