Is there a cleaner way in the Entity Framework to convert an object type to a string representation for storage?

A very insignificant thing in fact, but it offends me a little, so I thought I would ask. I have a POCO Setting element, and I'm using the first code approach to the Entity Framework.

public class Setting { [Required] [MaxLength(128)] public string Name { get; set; } [Required] public Type Type { get; set; } // Added to support the storing of Type in the database via Entity Framework. // Really would be nice to find a cleaner way but this isn't actually so bad. public string TypeString { get { return Type.ToString(); } set { Type = Type.GetType(value); } } public string Value { get; set; } } 

As you can see for use in the code, I would like to use the Type object, but to save it I added the TypeString property. Through DbModelBuilder I will hide the Type property.

  protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder .Entity<Setting>() .HasKey(e => e.Name) .Property(e => e.Name) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); modelBuilder .Entity<Setting>() .Ignore(e => e.Type); modelBuilder .Entity<Setting>() .Property(e => e.TypeString) .HasColumnName("Type"); base.OnModelCreating(modelBuilder); } 

I'm just wondering if there is a way to define a custom property mapping instead of adding this extra property to my entity.

UPDATE

My reasoning is that I just wanted the quick and easy way for developers to be able to set up some simple settings by logging in, and it was too late, and it seemed like a quick solution, allowing several settings of various types.

I believe that if I wanted to get strongly typed settings, I would probably look at the general implementation of the settings, for example below:

 public class Setting<T> { [Required] [MaxLength(128)] public string Name { get; set; } public T Value { get; set; } } 

Although I do not believe that this will work well with the Entity Framework.

Partially, although I am also curious that for some applications I have several clients or interested parties who may request slightly different validation rules. Thus, we usually implement and link and create an implementation on clients or client collections. So that we can more easily add clients and customize our rules, we save which interface implementation to create for each client. Thus, the stored type information has proven extremely useful in these cases.

It is also nice to learn and understand the ways in which I can with great pleasure develop an application, reducing the need to think about how I persist in this, or it will work great with the Entity Framework as much as possible.

+1
source share
1 answer

I don’t know how to persist in Type , but it might seem a little better:

 public class Settings { public Type Type { get { return Type.GetType(_TypeString); } set { _TypeString = value.ToString(); } } // Backing Field protected virtual string _TypeString { get; set; } } 

Then you just need to map the protected _TypeString property (solution from here ):

 public static StringPropertyConfiguration Property<T>(this EntityTypeConfiguration<T> mapper, String propertyName) where T : class { Type type = typeof(T); ParameterExpression arg = Expression.Parameter(type, "x"); Expression expr = arg; PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); expr = Expression.Property(expr, pi); LambdaExpression lambda = Expression.Lambda(expr, arg); Expression<Func<T, String>> expression = (Expression<Func<T, string>>)lambda; return mapper.Property(expression); } 

Then in ModelBuilder :

 modelBuilder .Entity<Setting>() .Property("_TypeString") .HasColumnName("Type"); 
+2
source

All Articles