Tôi cần sử dụng kiểu "datetime2" của Sql Server trên tất cả DateTime và DateTime? các thuộc tính của tất cả các đối tượng thực thể của tôi. Việc này thường được thực hiện bằng API thông thạo như sau:Một khung Entity thông minh hơn Codefirst thông thạo API
modelBuilder.Entity<Mail>().Property(c => c.SendTime).HasColumnType("datetime2");
Tuy nhiên, tôi không muốn thực hiện việc này theo cách thủ công cho từng và mọi trường DateTime trong từng loại thực thể. (Tôi không có một kiểu cơ sở chung mà tất cả các thuộc tính DateTime có thể được đặt, vì các thuộc tính DateTime là cụ thể cho các kiểu thực thể mà chúng được định nghĩa).
Câu hỏi ngắn: Tùy chọn của tôi là gì? Câu hỏi dài: Tôi đang cân nhắc việc sử dụng sự phản chiếu và làm cho sự thiếu thốn, nhưng nó rất lộn xộn vì dường như API thông thạo không thực sự phù hợp với loại công việc này vì tôi phải gọi các phương thức API thông thạo chung thông qua phản ánh. Đây là nỗ lực lộn xộn của tôi:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
var genericEntityMethod = modelBuilder.GetType().GetMethod("Entity");
var entityTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.GetInterface("IEntity") != null);
foreach (var t in entityTypes) {
var props = t.GetProperties().Where(p =>
p.GetSetMethod() != null &&
p.GetGetMethod() != null &&
(p.PropertyType == typeof (DateTime) ||
p.PropertyType == typeof(DateTime?)));
foreach (var propertyInfo in props) {
var entityMethod = genericEntityMethod.MakeGenericMethod(t.GetType());
var entityTypeConfiguration = entityMethod.Invoke(modelBuilder,null);
var lambdaExpression = Expression.Lambda(Expression.MakeMemberAccess(Expression.Parameter(t), propertyInfo), Expression.Parameter(t));
//var propertyMethod = entityTypeConfiguration.GetType().GetMethod("Property"); // Cant get this to work
var propertyMethods = entityTypeConfiguration.GetType().GetMethods().Where(m => m.ReturnType == typeof(DateTimePropertyConfiguration)).ToList();
var propertyMethod = propertyInfo.PropertyType == typeof (DateTime) ? propertyMethods[0] : propertyMethods[1];
var dateTimePropertyConfiguration = propertyMethod.Invoke(entityTypeConfiguration, new object[] {lambdaExpression});
var hasColumnTypeMethod = entityTypeConfiguration.GetType().GetMethod("HasColumnType");
hasColumnTypeMethod.Invoke(dateTimePropertyConfiguration, new object[]{ "datetime2" });
}
}
base.OnModelCreating(modelBuilder);
}
này thất bại trong dòng này:
var dateTimePropertyConfiguration = propertyMethod.Invoke(entityTypeConfiguration, new object[] {lambdaExpression});
với lỗi này (Entities.Order là một trong những đối tượng thực thể của tôi có một tài sản DateTime):
Object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Entities.Order,System.Nullable`1[System.DateTime]]]' cannot be converted to type 'System.Linq.Expressions.Expression`1[System.Func`2[System.RuntimeType,System.Nullable`1[System.DateTime]]]
Bất cứ ai có thể giúp tôi với cách tiếp cận phản chiếu này hay tốt hơn, chỉ cho tôi cách đơn giản hơn để tránh phải viết rất nhiều công cụ api thông thạo bằng tay?