其他分享
首页 > 其他分享> > (Entity Framework Core入门)四、EFCore的模拟离线修改

(Entity Framework Core入门)四、EFCore的模拟离线修改

作者:互联网

1.生命周期说明(注入,可以参考前面(初识MVC CORE)一、初识管道和生命周期

 

1)Scop:每次Http请求都去生成一个MyContext实例

2)Transient:每次需要的时候,都会创建一个MyContext实例

3)Singleton:一旦被创建,就会一直使用这个实例,直到应用停止

 

2.修改(修改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"));
                },ServiceLifetime.Transient);

        }

 

3.修改HomeController.cs

 

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

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

        public IActionResult Index()
        {

            var pro2 = _context.Provinces.FirstOrDefault();
            if (pro2 != null)
            {
                _context2.Provinces.Update(pro2);
                _context2.SaveChanges();
            }

            var pro = new Province
            {
                Name = "四川",
                Population = 5000,
                Cities = new List<City>
                {
                    new City{AreaCode="0001",Name = "重庆" },
                    new City{AreaCode="0002",Name = "重庆2" }
                }
            };

            _context.Provinces.Add(pro);
            _context.SaveChanges();

            //预加载查询
            var provincee = _context.Provinces
                .Include(x => x.Cities)
                .First(x=>x.Cities.Any());

            var city = provincee.Cities[0];
            city.Name += " Updated";

            //离线修改
            _context2.Entry(city).State = EntityState.Modified;

            return View();
        }


    }
}

 

4.添加断点运行,确认是否同一个对象

 

注:离线修改将除主键以外的所有属性值进行Update一遍

 

 感谢:Dave

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

https://v.qq.com/x/page/g0768g6ca62.html

 

标签:Core,Provinces,context2,离线,Entity,context,using,options,MyContext
来源: https://www.cnblogs.com/dzw159/p/10808276.html