This project is read-only.

EFDAL: ModifiedBy, -Date updated?

Feb 1, 2011 at 7:07 PM
Edited Feb 1, 2011 at 8:39 PM

Hi,

I am using the EFDAL voor a project but when using the following code:

-------------------- partial class StamboekCoreEntities ----------

        public virtual void Edit(Ansem.StamboekCore.EFDAL.Entity.Persoon entity)
        {            
            entity.ModifiedBy = _contextStartup.Modifer;
            entity.ModifiedDate = DateTime.Now;
            //base.AttachTo("Persoon", entity);
        }

----------- public class PersoonCollection : IPersoonCollection --------

        ///<summary>
        ///</summary>
        public PersoonCollection(string modifier)
        {
            _contextStartup = new ContextStartup(modifier);
            db = new StamboekCoreEntities(_contextStartup);
        }


---------------public class PersoonController : Controller -------------
CONSTRUCTOR

        public PersoonController()
            : this(new PersoonCollection(System.Web.HttpContext.Current.User.Identity.Name)) {
        }

--------- Action ---------
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, PersoonViewModel persoonViewModel)
        {
            int? adresId = persoonViewModel.AdresViewModel.Adres.AdresId;
            var persoon = _persoonCollection.SelectUsingPk(id);
            var adres = persoon.AdressenAdresList.SingleOrDefault(x => x.Id == adresId);
            var persoon2 = persoon.Clone();
            var adresChanged = false;
            try
            {
                if (ModelState.IsValid)
                {                             
                    UpdateModel(persoon, "Persoon");

                    if (adres == null) // Toevoegen
                    {
                        adresChanged = true;
                        adres = new Adres { PersoonFK = id
                           // , CreatedBy = System.Web.HttpContext.Current.User.Identity.Name
                           // , ModifiedBy = System.Web.HttpContext.Current.User.Identity.Name
                            };                   
                        UpdateModel(adres, "AdresViewModel.Adres");
                        persoon.AdressenAdresList.Add(adres);
                    }
                    else // Edit
                    {
                        var adres2 = adres.Clone();
                        UpdateModel(adres, "AdresViewModel.Adres");
                        if (!adres.IsEquivalent(adres2)) // Alleen als er wat gewijzigd is ivm AuditTrail
                        {
                            adresChanged = true;
                         //   adres.ModifiedBy = System.Web.HttpContext.Current.User.Identity.Name;
                         //   adres.ModifiedDate = DateTime.Now;
                        }
                    }

                    if (!persoon.IsEquivalent(persoon2) || adresChanged)
                    {
                        _persoonCollection.Edit(persoon);
                        _persoonCollection.Save();
                    }
                        
                    return RedirectToAction("Details", new {id = id});
                }
                return View(GetPersoonViewModel(persoon));
            }
            catch (Exception ex)
            {
                return View(GetPersoonViewModel(persoon));
            }
        }
When the code is exexuted the update of the ModiefiedBy and Date of the table Persoon is going well, but for Adres it isn't
When I remove the comment for the modifiedBy and -Date it is going well for the table Adres, but there has to be an easier way. I guess. Does someone know ?
Feb 2, 2011 at 9:48 PM

I do not really understand what is wrong. Are you saying when you set the ModfiedDate it does not save? All of this should be automatic. There is no reason to set the Created/Modified dates unless you want them to be different than right now. The database stored procedures actually handle all of this.

Feb 3, 2011 at 6:53 PM

When I edit an existing record and I don't set manually ModifiedDate and ModifiedBy the value of the fields ModifiedDate and ModifiedBy don't change.
When Adding a record it goes well for the Persoon table, but in the Adres table, the fields ModifiedBy and CreatedBy have the value NULL

Add:

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(PersoonViewModel persoonViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var persoon = persoonViewModel.Persoon;
                    _persoonCollection.Add(persoon);
                    persoon.AdressenAdresList.Add(persoonViewModel.AdresViewModel.Adres);
                    _persoonCollection.Save();

<snip/>

Edit:

See start topic, below ------ action ----------

Feb 20, 2011 at 8:58 PM

This has been identified and corrected for the next version. Each time a column is set the modified date should automatically be updated. This was not happening the EFDAL like it was in the NHDAL. As I said, this will be in the next post.