(Entity Framework Core入门)三、EFCore的CURD

承接上一章笔记

我们想要在前台使用CURD的数据操作和展现

 

1.添加/查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspEFCore.Data;
using AspEFCore.Domain.Model;
using Microsoft.AspNetCore.Mvc;

namespace AspEFCore.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly MyContext _context;
        public HomeController(MyContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
            //添加
            Province pro = new Province
            {
                Name = "北京",
                Population = 2000
            };

            _context.Provinces.Add(pro);
            _context.SaveChanges();
             
            //查询
            var provinces = _context.Provinces.Where(p=>p.Name == "北京");
            

            return View();
        }

    }
}

 2.设置默认启动项(修改Startup.cs)

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});

            #region 配置路由

            //将错误显示在页面
            app.UseStatusCodePages();
            //跳转到指定错误位置
            //app.UseStatusCodePagesWithRedirects("/Error");         

            //加载wwwroot里头的文件(使用此可以加载出来)
            app.UseStaticFiles();

            //配置路由
            app.UseMvc(routes => {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            #endregion

        }

 

 3.选择项目启动(为了在控制台看到数据)

 

4.查看控制台

 

4.批量添加

//批量添加
            Province pro1 = new Province
            {
                Name = "北京",
                Population = 1500
            };
            Province pro2 = new Province
            {
                Name = "北京",
                Population = 2500
            };
            Province pro3 = new Province
            {
                Name = "北京",
                Population = 3500
            };
            //_context.Provinces.AddRange(pro1,pro2,pro3);
            _context.Provinces.AddRange(new List<Province>
            {
                pro1, pro2, pro3
            });


        _context.SaveChanges();

 

 

5.修改

//修改
            var pro4 = _context.Provinces.Where(p=>p.Id == 1).FirstOrDefault();
            if (pro4 != null)
            {
                pro4.Name = "厦门";
                pro4.Population += 1000;
            }
            _context.SaveChanges();

 

 6.删除

            //删除
            var pro5 = _context.Provinces.Where(p => p.Id == 1).FirstOrDefault();

            //批量删除
            //_context.Provinces.RemoveRange(pro5);

            _context.Provinces.Remove(pro5);
            _context.SaveChanges();

 

注:

1)显示插入数据的敏感内容

修改Startup.cs中的ConfigureServices

 public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options => 
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);

            services.AddDbContext<MyContext>(
                options=>
                {
                    //显示敏感字(插入的数据--例如:中文内容)
                    options.EnableSensitiveDataLogging(true);

                    //获取数据连接串
                    //options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=AspEFCoreDemo;Trusted_Connection=True;");
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                });

        }

 

结果:

 

 

2)使用存储

 

 

 

感谢:Dave

参阅: https://v.qq.com/x/page/i0767euw2l1.html

 

posted @ 2019-05-03 14:59  蜗牛的礼物  阅读(874)  评论(0编辑  收藏  举报