【问题标题】:Pluggable conventions in EF 5?EF 5 中的可插拔约定?
【发布时间】:2012-09-12 17:19:59
【问题描述】:

是否从 EF 5 版本中删除了可插拔约定,我在预发行版中看到了它?寻找一种强制 ef 代码按约定使用 datetime2 的方法,因此不需要显式映射每个实体的每一列。

【问题讨论】:

    标签: .net entity-framework entity-framework-5


    【解决方案1】:

    遇到了类似的问题。我有一个通用存储库,它的 BaseEntity 与您的相似。

    public abstract class BaseEntity
    {
        public int Id { get; set; }
        public DateTime Created { get; set; }
        public DateTime Updated { get; set; } 
    }
    

    然后每个实体都是从基础派生的。

    public class MyEntity: BaseEntity
    {
        public string Name{ get; set; }
        public int prop1 { get; set; }
        public int prop2 { get; set; }
    }
    

    我最终使用如下的列属性设置了 BaseEntity 的 Created 和 Updated 属性:

    public abstract class BaseEntity
    {
        [Key, Column(Order = 0)]
        public int Id { get; set; }
        [Column(TypeName = "datetime2")]
        public DateTime Created { get; set; }
        [Column(TypeName = "datetime2")]
        public DateTime Updated { get; set; } 
    }
    

    我在 (http://dreadjr.blogspot.com/2012/09/entity-framework-5-code-first-datetime2.html) 上查看了您的解决方案,它看起来也不错,但它需要有一个每个实体的特定配置类。不管怎样,我想我会和你分享这个。

    【讨论】:

      【解决方案2】:

      最终创建了 EntityTypeConfiguration 的派生类。

      public class CustomEntityTypeConfiguration<T> : EntityTypeConfiguration<T> where T : EntityBase
      {
          public TEntityTypeConfiguration()
          {
              this.Property(t => t.CreatedDate).HasColumnType("datetime2");
          }
      }
      

      那么每个实体配置都只是继承自 this 而不是 EntityTypeConfiguration。

      【讨论】:

        猜你喜欢
        • 2012-01-15
        • 2014-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-17
        • 2013-07-19
        相关资源
        最近更新 更多