CSLA Entity Framework 4.1 Code First

CSLA Entity Framework 4.1 Code First

Old forum URL: forums.lhotka.net/forums/t/10376.aspx


sergioadh posted on Monday, May 16, 2011

Hi, I'm trying to make this work, but I haven't got it correctly, I'm using my CSLA entities to create my database with Entity Framework 4.1.

 

But I'm having a problem with the declaration and managing of child objects.

I can't seem to retrieve my entities 'Maestro' and I don't know what am I doing wrong, because I have a class Alumno which can retrieve correctly its collections but the collections object has an object which I can't seem to retrieve

Altough I do seem to save it correctly into DB

Class Clase

    [Serializable]

    public class Clase : BusinessBase<Clase>

    {

        #region Business Methods

 

        private static PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(Clase), new PropertyInfo<Guid>("Id"));

        [Key]

        public Guid Id

        {

            get { return GetProperty(IdProperty); }

            set { SetProperty(IdProperty, value); }

        }

 

        private static PropertyInfo<string> NombreProperty = RegisterProperty(typeof(Clase), new PropertyInfo<string>("Nombre", "Nombre de la Clase"));

        [Display(Name = "Nombre de la Clase")]

        [Required(ErrorMessage = "Nombre de Clase requerido")]

        public string Nombre

        {

            get { return GetProperty(NombreProperty); }

            set { SetProperty(NombreProperty, value); }

        }

 

        private static PropertyInfo<Maestro> MaestroProperty = RegisterProperty(typeof(Clase), new PropertyInfo<Maestro>("Maestro", "Maestro de la Clase"));

        [Display(Name = "Maestro de la Clase")]

        [Required(ErrorMessage = "La clase requiere de un Maestro")]

        public Maestro Maestro

        {

            get

            {

                if (!FieldManager.FieldExists(MaestroProperty))

                {

                    LoadProperty(MaestroProperty, Maestro.GetMaestro(this));

                }

                return GetProperty(MaestroProperty);

            }

            set { SetProperty(MaestroProperty, value); }

        }

 

        private static PropertyInfo<Sesiones> SesionesProperty = RegisterProperty(typeof(Clase), new PropertyInfo<Sesiones>("Sesiones", "Horario de la Clase"));

        [Display(Name = "Horario de la Clase")]

        public virtual Sesiones Sesiones

        {

            get

            {

                if (!(FieldManager.FieldExists(SesionesProperty)))

                    LoadProperty(SesionesProperty, Sesiones.NewSesiones());

                return GetProperty(SesionesProperty);

            }

        }

 

        private static PropertyInfo<Equipos> EquiposProperty = RegisterProperty(typeof(Clase), new PropertyInfo<Equipos>("Equipos", "Equipos de la Clase"));

        [Display(Name = "Equipos de la Clase")]

        public virtual Equipos Equipos

        {

            get

            {

                if (!(FieldManager.FieldExists(EquiposProperty)))

                    LoadProperty(EquiposProperty, Equipos.NewEquipos());

                return GetProperty(EquiposProperty);

            }

        }

 

        #endregion

 

        #region  Factory Methods

 

        public static Clase NewClase()

        {

            return DataPortal.CreateChild<Clase>();

        }

 

        public static Clase GetClase(Guid Id)

        {

            return DataPortal.FetchChild<Clase>(new SingleCriteria<Clase, Guid>(Id));

        }

 

        public static Clase GetClase(Clase data)

        {

            return DataPortal.FetchChild<Clase>(new SingleCriteria<Clase, Clase>(data));

        }

 

        private Clase()

        { /* require use of factory methods */ MarkAsChild(); }

 

        #endregion

 

        #region Data Access

 

        [RunLocal()]

        protected override void Child_Create()

        {

            LoadProperty(IdProperty, Guid.NewGuid());

            BusinessRules.CheckRules();

        }

 

        [Transactional(TransactionalTypes.TransactionScope)]

        private void Child_Insert(object parent)

        {

            var ctx = Horario.GetContexto();

            Alumno Alumno = ctx.Alumnos.Find(((Alumno)parent).Id);

            Alumno.Clases.Add(this);

            ctx.SaveChanges();

 

            FieldManager.UpdateChildren(this);

        }

 

        [Transactional(TransactionalTypes.TransactionScope)]

        private void Child_Update(object parent)

        {

            var ctx = Horario.GetContexto();

            var Clase = ctx.Clases.Find(ReadProperty(IdProperty));

            Clase.Nombre = ReadProperty(NombreProperty);

            ctx.SaveChanges();

 

            FieldManager.UpdateChildren(this);

        }

 

        private void Child_Fetch(SingleCriteria<Clase, Clase> criteria)

        {

            LoadProperty(IdProperty, criteria.Value.Id);

            LoadProperty(NombreProperty, criteria.Value.Nombre);

            LoadProperty(MaestroProperty, Maestro.GetMaestro(criteria.Value.Id));

            LoadProperty(SesionesProperty, Sesiones.GetSesiones(criteria.Value.Sesiones.ToArray()));

            LoadProperty(EquiposProperty, Equipos.GetEquipos(criteria.Value.Equipos.ToArray()));

        }

 

        private void Child_Fetch(SingleCriteria<Clase, Guid> criteria)

        {

            var ctx = Horario.GetContexto();

            var data = ctx.Clases.Find(criteria.Value);

            LoadProperty(IdProperty, data.Id);

            LoadProperty(NombreProperty, data.Nombre);

            LoadProperty(MaestroProperty, data.Maestro);

            LoadProperty(SesionesProperty, Sesiones.GetSesiones(data.Sesiones.ToArray()));

            LoadProperty(EquiposProperty, Equipos.GetEquipos(data.Equipos.ToArray()));

        }

 

        #endregion

    }

 

Class Maestro

    [Serializable()]

    public class Maestro : IPersona

    {

        #region  Factory Methods

 

        public static Maestro NewMaestro()

        {

            return DataPortal.CreateChild<Maestro>();

        }

 

        internal static Maestro GetMaestro(Clase parent)

        {

            return DataPortal.FetchChild<Maestro>(new SingleCriteria<Maestro, Guid>(parent.Id));

        }

 

        internal static Maestro GetMaestro(Guid id)

        {

            return DataPortal.FetchChild<Maestro>(new SingleCriteria<Maestro, Guid>(id));

        }

 

        private Maestro()

        { /* require use of factory methods */ MarkAsChild(); }

 

        #endregion

 

        #region Data Access

 

        protected override void Child_Create()

        {

            LoadProperty(IdProperty, Guid.NewGuid());

            BusinessRules.CheckRules();

        }

 

        [Transactional(TransactionalTypes.TransactionScope)]

        private void Child_Insert(object parent)

        {

            var ctx = Horario.GetContexto();

            var Clase = ctx.Clases.Find(((Clase)parent).Id);

            Clase.Maestro = this;

            ctx.SaveChanges();

 

            FieldManager.UpdateChildren(this);

        }

 

        [Transactional(TransactionalTypes.TransactionScope)]

        private void Child_Update(object parent)

        {

            var ctx = Horario.GetContexto();

            var Clase = ctx.Clases.Find(((Clase)parent).Id);

            Clase.Maestro.Nombre = ReadProperty(NombreProperty);

            Clase.Maestro.Correo = ReadProperty(CorreoProperty);

            Clase.Maestro.Telefono = ReadProperty(TelefonoProperty);

            ctx.SaveChanges();

 

            FieldManager.UpdateChildren(this);

        }

 

        private void Child_Fetch(SingleCriteria<Maestro, Maestro> criteria)

        {

            LoadProperty(IdProperty, criteria.Value.Id);

            LoadProperty(NombreProperty, criteria.Value.Nombre);

            LoadProperty(CorreoProperty, criteria.Value.Correo);

            LoadProperty(TelefonoProperty, criteria.Value.Telefono);

        }

 

        private void Child_Fetch(SingleCriteria<Maestro, Guid> criteria)

        {

            var ctx = Horario.GetContexto();

            var data = ctx.Clases.Find(criteria.Value).Maestro;

            LoadProperty(IdProperty, data.Id);

            LoadProperty(NombreProperty, data.Nombre);

            LoadProperty(CorreoProperty, data.Correo);

            LoadProperty(TelefonoProperty, data.Telefono);

        }

 

        #endregion

    }

 

Class IPersona

    [Serializable()]

    public class IPersona : BusinessBase<IPersona>

    {

        #region Business Methods

 

        protected static PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(IPersona), new PropertyInfo<Guid>("Id"));

        [Key]

        public Guid Id

        {

            get { return GetProperty(IdProperty); }

            set { SetProperty(IdProperty, value); }

        }

 

        protected static PropertyInfo<string> NombreProperty = RegisterProperty(typeof(IPersona), new PropertyInfo<string>("Nombre", "Nombre de Usuario"));

        [Display(Name = "Nombre de Usuario")]

        [Required(ErrorMessage = "Nombre de Usuario requerido")]

        public string Nombre

        {

            get { return GetProperty(NombreProperty); }

            set { SetProperty(NombreProperty, value); }

        }

 

        protected static PropertyInfo<string> CorreoProperty = RegisterProperty(typeof(IPersona), new PropertyInfo<string>("Correo", "Correo Electrónico"));

        [Display(Name = "Correo Electrónico")]

        [DataType(System.ComponentModel.DataAnnotations.DataType.EmailAddress)]

        [RegularBLOCKED EXPRESSION; }

        }

 

        #endregion

    }

sergioadh replied on Monday, May 16, 2011

Ok I'm mistaken I had

 

private static PropertyInfo<Maestro> MaestroProperty = RegisterProperty(typeof(Clase), new PropertyInfo<Maestro>("Maestro", "Maestro de la Clase"));

        [Display(Name = "Maestro de la Clase")]

        [Required(ErrorMessage = "La clase requiere de un Maestro")]

        public Maestro Maestro

        {

            get

            {

                if (!FieldManager.FieldExists(MaestroProperty))

                    LoadProperty(MaestroProperty, Maestro.NewMaestro());

                return GetProperty(MaestroProperty);

            }

            set { SetProperty(MaestroProperty, value); }

        }

 

The thing is that I CAN retrieve from my parent class it's Clases (which is a list of Clase) with no problem but when I retrieve each Clase from my Clases when they try to get Maestro it creates a new one.

Copyright (c) Marimer LLC