• EFCore实践测试一


    .NET的生态圈感觉还是太闭塞了,反观thinkphp,python,想学习的话,大把资料可以参考,也有大把视频可以学习,.NET这也收费,那也收费,不是说不尊重别人的劳动成果,而是我觉得大家有必要先把氛围带动起来,建立一个良好的生态圈,更多人学会,更多人会使用,加入到.NET里面来,才会有更好的发展,然后再是其他,我百度找了很多.NETCORE的EFCORE资料,感觉不多,我自己实践做点例子,有需要的可以试试

    1.新建一个控制台,只为测试EF,如果要是测试MVC,相关注册可参考如下

    控制台,引入Microsoft.EntityFrameworkCore,Microsoft.EntityFrameworkCore.sqlserver  ------------NUGET

    MVC:

    Startup中

    public Startup(IConfiguration _configuration)
            {
                configuration = _configuration;
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
                //https://blog.csdn.net/u011511086/article/details/80609548
                services.AddMvc();
                services.AddEntityFrameworkSqlServer();
                //services.AddEntityFrameworkSqlServer();
                var connectionString = configuration["ConnectionStrings:DefaultConnection"];
                //dbContextOptions.UseSqlServer(connectionString, b => b.UseRowNumberForPaging());providerOptions => providerOptions.CommandTimeout(120)
                services.AddDbContext<DataContext>(options =>
                {
                    options.UseSqlServer(connectionString, providerOptions =>
                      {
                          //providerOptions.UseRowNumberForPaging(true);
                          providerOptions.CommandTimeout(120);
    
                      });
                    
                });
                services.AddScoped<IService<t_company>, t_companyService>();
                //services.AddControllersWithViews();
    
            }
     public class DataContext:DbContext
        {
            public DataContext(DbContextOptions<DataContext> options)
              : base(options)
            {
               
            }
    
            //protected override void OnModelCreating(ModelBuilder modelBuilder)
            //{
            //    var assembly = Assembly.GetExecutingAssembly();
            //    foreach (Type type in assembly.ExportedTypes)
            //    {
            //        if (type.IsClass && type != typeof(EntityBase) && typeof(EntityBase).IsAssignableFrom(type))
            //        {
            //            var method = modelBuilder.GetType().GetMethods().Where(x => x.Name == "Entity").FirstOrDefault();
    
            //            if (method != null)
            //            {
            //                method = method.MakeGenericMethod(new Type[] { type });
            //                method.Invoke(modelBuilder, null);
            //            }
            //        }
            //    }
    
            //    base.OnModelCreating(modelBuilder);
            //}
            public virtual DbSet<t_company> t_company { get; set; }
            public virtual DbSet<t_dep> t_dep { get; set; }
            public virtual DbSet<t_role> t_role { get; set; }
            public virtual DbSet<t_user> t_user { get; set; }
            public DbSet<ThinkAsp.Web.Model.t_test> t_test { get; set; }
        }

    appsettings.json中设置连接字符串:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "ConnectionStrings": {
        "DefaultConnection": "Data Source = .;Initial Catalog = XXXX;User Id = sa;Password = 123456;"
      }
    }

    2.继续控制台

    using Microsoft.EntityFrameworkCore;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Text;
    
    namespace EFStudy
    {
        public class BloggingContext : DbContext
        {
            public DbSet<Blog> Blogs { get; set; }
            public DbSet<Post> Posts { get; set; }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                //optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);
                optionsBuilder.UseSqlServer("Data Source = .;Initial Catalog = Test;User Id = sa;Password = 123456;");
            }
    
            public class Blog
            { 
                public int BlogId { get; set; }
                public string Url { get; set; }
                public int Rating { get; set; } 
                public List<Post> Posts { get; set; }
            }
            public class Post 
            { 
                public int PostId { get; set; }
                public string Title { get; set; }
                public string Content { get; set; } 
                //public int BlogId { get; set; } 
                public Blog Blog { get; set; }
            }
        }
    }

    PM:Install-Package Microsoft.EntityFrameworkCore.Tools

    失败用nuget安装

    add-migration initcitydata01

    update-database

    执行完毕开始操作:

    待续

  • 相关阅读:
    将html转换成image图片png格式
    maven 发布打包部署 命令
    javap 指令集
    国内maven仓库地址
    五行大义
    oracle
    【Centos linux系统】命令行(静默)安装oracle 11gR2
    windows安装mysql-5.7压缩版详细教程
    k8s入门系列之扩展组件(一)DNS安装篇
    k8s入门系列之集群安装篇
  • 原文地址:https://www.cnblogs.com/wangchuang/p/12317546.html
Copyright © 2020-2023  润新知