예제 #1
0
        public int SaveChanges()
        {
            var context = DbContentFactory.GetCurrentDbContent();

            var saved = false;

            while (!saved)
            {
                try
                {
                    // Attempt to save changes to the database
                    context.SaveChangesAsync();
                    saved = true;
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    foreach (var entry in ex.Entries)
                    {
                        var databaseValues = entry.GetDatabaseValues();

                        // Refresh original values to bypass next concurrency check
                        entry.OriginalValues.SetValues(databaseValues);
                    }
                }
            }

            return(1);
        }
예제 #2
0
        private void InitData(IServiceProvider serviceProvider)
        {
            var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var context = serviceScope.ServiceProvider.GetService <DataContext>();

            DbContentFactory.Initialize(context);//调用静态类方法注入
        }
예제 #3
0
 /// <summary>
 /// 拿到当前EF的上下文对象,然后进行把修改实体进行一个整体提交(简单说该方法就是让上下文提交)
 /// </summary>
 /// <returns></returns>
 public int SaveChanges()
 {
     try
     {
         return(DbContentFactory.GetCurrentDbContent().SaveChanges());//需要添加对Entity Framework.dll的引用
     }
     catch (Exception)
     {
         return(1);
     }
 }
예제 #4
0
        public static void Main(string[] args)
        {
            var host  = CreateWebHostBuilder(args).Build();
            var scope = host.Services.CreateScope();

            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <Models.DataContext>();
                    DbContentFactory.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }
            host.Run();
        }
예제 #5
0
 public DataContext GetDbContent()
 {
     return(DbContentFactory.GetCurrentDbContent());
 }
예제 #6
0
 //----封装SaveChanages----减少用户与数据库之间的会话----单元工作----批量提交----提高性能
 //将提交数据操作从Dal层提升到Bll层
 /// <summary>
 /// DbContentFactory.GetCurrentContext()获取上下文
 /// </summary>
 /// <returns></returns>
 public int SaveChanges()
 {
     return(DbContentFactory.GetCurrentContext().SaveChanges());
 }