Is there a way to apply value conversion on backing fields? - entity-framework

Question: Is there a way in Entity Framework Core 3.0 to apply value conversion on backing fields?
Context:
Let's say I have a Blog entity that contains a list of string values representing post ids. I want to avoid the need to have a join entity/table (as described here) so I do a conversion to store the list as a string in the DB. I also want to protect my model from being modified directly through the PostIds property, so I want to use a backing field for that (as described here).
Something like this:
public class Blog
{
public int BlogId { get; set; }
private readonly List<string> _postIds;
public IReadOnlyCollection<string> PostIds => _postIds;
public Blog()
{
_posts = new List<Post>();
}
}
And configuration of the context would look something like that:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configuring the backing field
modelBuilder.Entity<Blog>()
.Metadata
.FindNavigation(nameof(Blog.PostIds))
.SetPropertyAccessMode(PropertyAccessMode.Field);
// Trying to configure the value conversion, but that doesn't work...
modelBuilder.Entity<Blog>()
.Property(e => e.PostIds)
.HasConversion(v => string.Join(',', v),
v => v.Split(','));
}
Any ideas how this configuration could be achieved with the current version of Entity Framework (3.0)?

Related

Using Two Columns as Discriminator

Is it possible to use two columns as a discriminator. For instance, something like:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Job>()
.HasDiscriminator<string>("Type")
.HasValue<Models.SpecificJob>("attack");
modelBuilder.Entity<Job>()
.HasDiscriminator<string>("Domain")
.HasValue<Models.SpecificJob>("fire_nation");
}
Right now, it seems like it takes only the very last discriminator and ignores the first.
No. A Discriminator indicates the subtype, and EF (and .NET) do not support Multiple Inheritence.
But you can (and should) just use normal properties, one for the "Type" and one for the "Domain".
You can add "Getters" for filtered subsets of your entities. EG:
class Db : DbContext
{
public DbSet<Job> Jobs { get; set; }
public IQueryable<Job> AttackJobs => Jobs.Where(j => j.Type == "attack");
public IQueryable<Job> FireNationJobs => Jobs.Where(j => j.Domain == "fire_nation");
. . .

EF Core 2.0 Enums stored as string [duplicate]

This question already has answers here:
Does EF7 (EFCore) support enums?
(2 answers)
Closed 7 months ago.
I was able to store an enum as a string in the database.
builder.Entity<Company>(eb =>
{
eb.Property(b => b.Stage).HasColumnType("varchar(20)");
});
But when it comes time to query EF doesn't know to parse the string into an enum. How can I query like so:
context
.Company
.Where(x => x.Stage == stage)
This is the exception: Conversion failed when converting the varchar value 'Opportunity' to data type int
Value Conversions feature is new in EF Core 2.1.
Value converters allow property values to be converted when reading
from or writing to the database. This conversion can be from one value
to another of the same type (for example, encrypting strings) or from
a value of one type to a value of another type (for example,
converting enum values to and from strings in the database.)
public class Rider
{
public int Id { get; set; }
public EquineBeast Mount { get; set; }
}
public enum EquineBeast
{
Donkey,
Mule,
Horse,
Unicorn
}
You can use own conversion
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<Rider>()
.Property(e => e.Mount)
.HasConversion(
v => v.ToString(),
v => (EquineBeast)Enum.Parse(typeof(EquineBeast), v));
}
or Built-in converter
var converter = new EnumToStringConverter<EquineBeast>();
modelBuilder
.Entity<Rider>()
.Property(e => e.Mount)
.HasConversion(converter);
You can use this to convert all the Enum of all the properties of all the entities into a string and vice versa :
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
namespace MyApp
{
public class DatabaseContext : DbContext
{
public DbSet<UserContext> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Configure your model here
}
protected override void OnModelCreating(ModelBuilder model)
{
foreach (var entityType in model.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (property.ClrType.BaseType == typeof(Enum))
{
var type = typeof(EnumToStringConverter<>).MakeGenericType(property.ClrType);
var converter = Activator.CreateInstance(type, new ConverterMappingHints()) as ValueConverter;
property.SetValueConverter(converter);
}
}
}
}
}
}
You can do this much more easily with a one-liner attribute.
[Column(TypeName = "nvarchar(24)")]
public EquineBeast Mount { get; set; }
That's all you need to do! This is a string to enum value conversion by explicitly specifying the database column type as an attribute on the property. (Documentation)
I had gotten the same error as the OP because I was originally using the [MaxLength] attribute instead.
(The original question is for EF 2.0, and this feature starts in EF 2.1 as others mention in their answers. But google lead me here so I decided to add a useful answer.)

Is there a way to have class names be different from your table names?

We are using a database created several years ago, and would like to keep the table names the same.
All of our tables are named like: "tbl_Orders" but we would like the class names for the models / controllers / etc. to be Orders / OrdersController / etc. We are mapping the classes to our tables using Entity Framework.
Sorry if this has been asked before, I tried searching but came up empty handed...
Solution:
After some back and forth with Scott Chamberlain, we came to the conclusion that both answers are correct. I went ahead and marked Masoud's answer as accepted, because that is the route I went. Thank's to everyone who helped (especially Scott).
You can use the Table attribute or the fluent api to map between table names in your database and class names
[Table("tbl_Blogs")]
public class Blog
3rd party edit
Entity framework core offers the same option to map tablenames or columns
map tables names
map column names
The mapping can be done by using attributes
[Table("blogs")]
public class Blog
{
[Column("blog_id")]
public int BlogId { get; set; }
public string Url { get; set; }
}
or by using the fluent api
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable("blogs");
modelBuilder.Entity<Blog>()
.Property(b => b.BlogId)
.HasColumnName("blog_id");
}
You can use following code in your DbContext to map all your entities to your tables:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// TableNameConvention
modelBuilder.Types()
.Configure(entity =>
entity.ToTable("tbl_" + entity.ClrType.Name));
base.OnModelCreating(modelBuilder);
}
Working on EF Core 7.0(5.0+) and this one worked for me.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (var mutableEntityType in modelBuilder.Model.GetEntityTypes())
{
// check if current entity type is child of BaseModel
if (mutableEntityType.ClrType.IsAssignableTo(typeof(BaseEntity)))
{
mutableEntityType.SetTableName($"tbl_{mutableEntityType.ClrType.Name.Pluralize()}");
}
}
base.OnModelCreating(modelBuilder);
}

In Entity Framework is there a cleaner way of converting an object type to a string representation for storage?

Very minor thing really but it bugs me slightly so I thought I'd ask. I have the POCO entity Setting and I'm using a code first approach to 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 code I'd like to actually be using the Type object but to store this I have ended up adding a TypeString property. Via the DbModelBuilder I then 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 just was wondering if there was a way of defining a custom property mapping instead of having to add that extra property to my entity.
UPDATE
My reasoning behind these was actually that I just wanted a quick and easy way for developers to be able to configure a few simple settings by logging in, and it was late and this seemed like a quick solution to allow for several settings of various types.
I suppose if if I wanted some strongly typed settings I'd probably look at a generic implementation of setting such as below:
public class Setting<T>
{
[Required]
[MaxLength(128)]
public string Name { get; set; }
public T Value { get; set; }
}
Though I don't believe that is something that will play nice with Entity Framework.
In part though I'm also curious as for some applications I have multiple clients or stakeholders who can each request slightly different validation rules. As such we usually implement and interface and create an implementation per clients or collections of clients. In order that we can more easily add clients and customise their rules we store which implementation of the interface to create for each client. So persisting type information has proved extremely useful in those cases.
Also it's nice to just explore and understand ways that I can quite happily develop an application whilst reducing the need to think how am I going to persist this, or is this going to play nice with Entity Framework as much as possible.
I'm not aware of any way to persist Type directly, but this may feel a bit 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 your ModelBuilder:
modelBuilder
.Entity<Setting>()
.Property("_TypeString")
.HasColumnName("Type");

EF Code First Readonly column

I am using EF Code first with database first approach.
"with Database.SetInitializer(null);"
My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these columns gets data via triggers.
Now What I want to do is to make this read only from EF Code first point of view. I.e. I want to be able to see the createddate and ameneded dates from my app but I dont want to amend these data.
I have tried using private modifiers on setter but no luck.When I try to add new data to the table it tried to enter DateTime.Max date to the database which throws error from SQL server.
Any idea?
You cannot use private modifiers because EF itself needs to set your properties when it is loading your entity and Code First can only do this when a property has public setter (in contrast to EDMX where private setters are possible (1), (2)).
What you need to do is mark your for CreatedDate with DatabaseGeneratedOption.Identity and your AmendDate with DatabaseGeneratedOption.Computed. That will allow EF to correctly load data from the database, reload data after insert or update so that entity is up to date in your application and at the same time it will not allow you to change the value in the application because the value set in the application will never be passed to the database. From an object oriented perspective it is not a very nice solution but from the functionality perspective it is exactly what you want.
You can do it either with data annotations:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedDate { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime AmendDate { get; set; }
Or with fluent API in OnModelCreating override in your derived context:
modelBuilder.Entity<YourEntity>()
.Property(e => e.CreatedDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<YourEntity>()
.Property(e => e.AmendDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
EF core 1.1 or later versions yes you can use read only property in poco classes. What you need to do is using backing-field.
public class Blog
{
private string _validatedUrl;
public int BlogId { get; set; }
public string Url
{
get { return _validatedUrl; }
}
public void SetUrl(string url)
{
using (var client = new HttpClient())
{
var response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();
}
_validatedUrl = url;
}
}
class MyContext : DbContext
{
public DbSet Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasField("_validatedUrl");
}
}
and fluent api...
modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasField("_validatedUrl")
.UsePropertyAccessMode(PropertyAccessMode.Field);
Take a look here..