【问题标题】:EF 5 Code First DbMigration automationEF 5 Code First DbMigration 自动化
【发布时间】:2012-11-08 17:49:10
【问题描述】:

我正在使用带有代码优先方法的 EF 5。

我无法将列的默认 DateTime 值设置为 (getdateutc())

我想要做的是让 EF 设置具有指定此值的表,而我发现可能的唯一方法是使用 DbMigrations(向上和向下方法)。还有其他方法吗?

我有一个像这样的基类

 public abstract class BASE_AUDITED : BASE
 {              
      [IgnoreDataMember, IgnoreMap]
      public DateTime Created { get; set; }

      [IgnoreDataMember, IgnoreMap]
      public DateTime Modified { get; set; }

      [MaxLength(50)]
      [IgnoreDataMember, IgnoreMap]
      public string CreatedBy { get; set; }

      [MaxLength(50)]
      [IgnoreDataMember, IgnoreMap]
      public string ModifiedBy { get; set; }
 }

 public abstract class BASE 
 {
      [IgnoreMap]
      public int id { get; set; }
 }

还有一堆继承自它的类。 我想做的是能够在 DBMigrations Up 方法中访问模型(使用fluentAPI 进行映射),并在那里编写一个循环来处理从Base_audited 继承的所有对象

换句话说,我试图避免为我添加的每个对象编写以下代码,

AlterColumn("T70_AccountService.CONTACT", "TestClmn", c => c.DateTime(nullable: false, defaultValueSql: "(getutcdate())"));

倒不如这样

 var types = ReflectionHelper.TypesImplementingInterface(typeof (BASE_AUDITED));
            foreach (var type in types)
            {
               var tableName = Context.FindTableNameFor(type);
               AlterColumn(tableName , "Created", c => c.DateTime(nullable: false, defaultValueSql: "(getutcdate())"));
               AlterColumn(tableName , "Modified", c => c.DateTime(nullable: false, defaultValueSql: "(getutcdate())"));

            }

简而言之 - 我在 DbMigrations Up 方法中找不到对象映射到哪个表..

 var tableName = Context.FindTableNameFor(type);

【问题讨论】:

  • C# 类名应为 UpperCamelCase。
  • 我也这么认为!告诉我们的建筑师:)

标签: c# .net entity-framework ef-code-first


【解决方案1】:

我已经实施的解决方案,似乎在一些开销(重新创建约束)的情况下工作正常,是使用迁移配置的“Seed”方法:

  protected override void Seed(AccountServiceEntities context)
        {
            //  This method will be called after migrating to the latest version.

            #region SET Defaults for Audit Fields

            var types = ReflectionHelper.GetEnumerableOfType<BASE_AUDITED>();
            foreach (var type in types)
            {
                var method = typeof(ContextExtensions).GetMethod("GetTableName");
                var generic = method.MakeGenericMethod(type.GetType());
                var table = generic.Invoke(this, new object[] { context });

                var constraintName = string.Format("DF__{0}__", type.GetType().Name);

                CreateDefaultCronstraint(context, table, constraintName, "Created");
                CreateDefaultCronstraint(context, table, constraintName, "Modified");

            }

            #endregion...

这是修改后的上下文扩展类,它从上下文中提取表名 公共静态类 ContextExtensions {

    public static string GetTableName<T>(this DbContext context) where T : class
    {
        var objectContext = ((IObjectContextAdapter)context).ObjectContext;
        return objectContext.GetTableName2<T>();
    }

    public static string GetTableName2<T>(this ObjectContext context) where T : class
    {
        var sql = context.CreateObjectSet<T>().ToTraceString();
        var regex = new Regex("FROM (?<table>.*) AS");
        var match = regex.Match(sql);

        var table = match.Groups["table"].Value;
        return table;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多