SQL column default value with Entity Framework

前端 未结 4 922
失恋的感觉
失恋的感觉 2020-12-17 10:09

I am trying to use Code-First EF6 with default SQL values.

For example, I have a \"CreatedDate\" column/property not null with a default in SQL of \"getdate()\"

4条回答
  •  感情败类
    2020-12-17 10:44

    [mysql]

    For those, who don't want to use computed and rewrite it after every db update, I wrote extension method for database partial class. Sure, there are thing that has to be improved or added, but for now it is enough for our using, enjoy.

    Take into account, that due to database_schema access it is not the fastest and also you need to have same entity name as table name (or rewrite it somehow).

        public static bool GetDBDefaults(object entity)
        {
            try
            {
                string table_name = entity.GetType().Name;
    
                string q = $"select column_name, column_default from information_schema.columns where column_default is not null and table_schema not in ('information_schema', 'sys', 'performance_schema', 'mysql') and table_name = '{table_name}' order by table_schema, table_name, ordinal_position;";
    
                List dbDefaults = new List();
                using (DatabaseModelFull db = new DatabaseModelFull())
                {
                    dbDefaults = db.Database.SqlQuery(q).ToList();
                }
    
                Type myType = entity.GetType();
                IList props = new List(myType.GetProperties());
                IList fields = new List(myType.GetFields());
    
                foreach (var dbDefault in dbDefaults)
                {
                    var prop = props.SingleOrDefault(x => x.Name == dbDefault.column_name);
    
                    if (prop != null)
                    {
                        if (dbDefault.column_default.Equals("CURRENT_TIMESTAMP"))
                            prop.SetValue(entity, System.Convert.ChangeType(DateTime.Now, prop.PropertyType));
                        else
                            prop.SetValue(entity, System.Convert.ChangeType(dbDefault.column_default, prop.PropertyType));
                        continue;
                    }
    
                    var field = fields.SingleOrDefault(x => x.Name == dbDefault.column_name);
    
                    if (field != null)
                    {
                        if (dbDefault.column_default.Equals("CURRENT_TIMESTAMP"))
                            field.SetValue(entity, System.Convert.ChangeType(DateTime.Now, field.FieldType));
                        else
                            field.SetValue(entity, System.Convert.ChangeType(dbDefault.column_default, field.FieldType));
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    
    
        public class DBDefaults
        {
            public string column_name { get; set; }
            public string column_default { get; set; }
        }
    

提交回复
热议问题