XAF Copy User Settings Controller

This is handy when copying one user’s settings to anothers

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.BaseImpl.EF;
using Microsoft.EntityFrameworkCore;
using SBD24.JobTalk.Module.BusinessObjects;
using SBD24.JobTalk.Module.Services;
using System.Linq;

namespace SBD24.JobTalk.Win.Controllers
{
       public partial class ApplicationUserController : ViewController
    {
        TargetObjectType = typeof(ApplicationUser);
        SimpleAction actImportUserSettings;
        SimpleAction actExportUserSettings;
        public ApplicationUser fromUser { get; set; }

        public ApplicationUserController()
        {
           // InitializeComponent();
             
            actExportUserSettings = new SimpleAction(this, "Export Settings", "View");
            actExportUserSettings.Execute += actExportUserSettings_Execute;
            actImportUserSettings = new SimpleAction(this, "Import Settings", "View");
            actImportUserSettings.Execute += actImportUserSettings_Execute;
        }

        private void actImportUserSettings_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            var toUser = View.CurrentObject as ApplicationUser;
            if(fromUser == null)
            {
                MessageBox.Show("No user has been selected to copy from.");
                return;
            }
            if(toUser == null)
            {
                MessageBox.Show("No user has been selected to copy to.");
                return;
            }
            var connectionString = ConnectionStringManager.Instance.ConnectionString;
            var optionsBuilder = new DbContextOptionsBuilder<JobTalkEFSmallCoreDbContext>().UseSqlServer(
                connectionString);
            using var db = new JobTalkEFSmallCoreDbContext(optionsBuilder.Options);
           
            var fromModelDifference = (from md in db.ModelDifferences
                where md.UserId.ToLower() == fromUser.ID.ToString().ToLower()
                select md).SingleOrDefault();
            if(fromModelDifference == null)
            {
                MessageBox.Show($"There are no differences to import from {fromUser.UserName}");
                return;
            }

            var toModelDifference = (from md in db.ModelDifferences
                                     where md.UserId.ToLower() == toUser.ID.ToString().ToLower()
                                     select md).SingleOrDefault();

            if (toModelDifference == null)
            {
                toModelDifference = new ModelDifference { UserId = toUser.ID.ToString(), ContextId = fromModelDifference.ContextId };
                db.ModelDifferences.Add(toModelDifference);

            }

            var fromOwnerId = fromModelDifference.ID;
            var toOwnerId = toModelDifference.ID;
            var fromAspects = db.ModelDifferenceAspects
                .Include(x => x.Owner)
                .Where(x => x.Owner.ID == fromOwnerId)
                .ToList();

            var toAspects = db.ModelDifferenceAspects
                .Include(x => x.Owner)
                .Where(x => x.Owner.ID == toOwnerId);
            //.ToList(); we need the actual entity, not the query


            foreach (var fromAspect in fromAspects)
            {
                var toAspect = toAspects.SingleOrDefault(x => x.Name == fromAspect.Name );
                if (toAspect == null)
                {
                    toAspect = new ModelDifferenceAspect { Owner = toModelDifference, Xml = fromAspect.Xml, Name = fromAspect.Name };
                    db.ModelDifferenceAspects.Add(toAspect);
                }
                else
                {
                    toAspect.Xml = fromAspect.Xml;
                    // mark toAspect as modified
                    db.Entry(toAspect).State = EntityState.Modified;
                }
            }
            db.SaveChanges();
            MessageBox.Show($"settings for {fromUser.UserName} have been copied to {toUser.UserName}");
        }

        private void actExportUserSettings_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            fromUser = View.CurrentObject as ApplicationUser;
            MessageBox.Show($"You have chosen to export settings for {fromUser.UserName}");
        }
    }
}