1

我为一张桌子设计了一个抽象类

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; }

   }
}

并从它扩展了一些类(TPH)我发现当属性定义为受保护时,它们不会在数据库中生成,它们应该是公共的(以上是受保护的其他 sts)。但我想从另一个命名空间隐藏上述属性并为它们使用不同的名称,例如:

   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        
            }
        }
   }
4

1 回答 1

2

一个简单的解决方案是改用访问表达式:

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;
        }
   }
}

在映射你去这样的事情:

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

进一步阅读:

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

于 2016-03-27T12:23:02.540 回答