1

I have designed an abstract class for one table

namespace Test.Data
{ 
   [Table("Parameter")]
   public abstract class Parameter 
   {
        [StringLength(200)]
        protected string title { get; set; }

        protected decimal? num { get; set; }

        public Int16 sts { get; set; }

   }
}

and have some classes extended from it (TPH) I found that when properties defined as PROTECTED, they will not generate in database and they should be public (above are protected else sts). But I would like to hide the above properties from another namespace and using different names for them, like:

   namespace Test.Data
   {
        public class Measure:Parameter
        {
            [NotMapped]
            public string Title { get { return ttl; } set { ttl = value; } }
        }
   }

   namsespace Test.Model
   {
        public class MeasureModel:Data.Measure
        {
            public void AddNew()
            {
               var m = new Data.Measure();
               m.Title="meter"; //other measure's properties shouldn't accessable here        
            }
        }
   }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Hamid
  • 817
  • 1
  • 13
  • 29

1 Answers1

2

A simple solution would be to use access expressions instead:

namespace Test.Data
{ 
   [Table("Parameter")]
   public partial abstract class Parameter 
   {
        [StringLength(200)]
        protected string title { get; set; }

        protected decimal? num { get; set; }

        public Int16 sts { get; set; }

   }
}

namespace Test.Data
{ 
   public partial abstract class Parameter 
   {
        public class PropertyAccessExpressions
        {
            public static readonly Expression<Func<Parameter, string>> Title = x => x.title;
        }
   }
}

In mapping you go for something like this:

.Property(Test.DataParameter.PropertyAccessExpressions.ID);

Further reading:

http://blog.cincura.net/232731-mapping-private-protected-properties-in-entity-framework-4-x-code-first/

bottaio
  • 4,963
  • 3
  • 19
  • 43