Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: How can I retrieve the value passed to the .HasMaxLength() method in EntityTypeConfiguration instance?

$
0
0
I've recently asked an SO question about this, but I thought I'd try here as well.

I'm doing some integration testing for CRUD operations, and using a fixture framework to quickly create instances of my Code First entities with random values. I'd like to be able to consume the EntityTypeConfiguration<T> instances somehow, so that I can honor things like max string length constraints which I've placed in these classes.

But, I've not been able to figure out a way to interrogate this class to get the information out. So far, I've tried inheriting the EntityTypeConfiguration<T> and creating a new method to intercept one of the Property() overload methods like this:
public class ReadableEntityTypeConfiguration<T> : EntityTypeConfiguration<T> where T : class
{
    public new StringPropertyConfiguration Property(Expression<Func<T, string>> propertyExpression)
    {
        return base.Property(propertyExpression);
    }
}
But I quickly realize I need to encapsulate the StringPropertyConfiguration class with something like this:
public class ReadableStringPropertyConfiguration : StringPropertyConfiguration
{
    public ReadableStringPropertyConfiguration()
    {
        // Cannot access internal constructor 'StringPropertyConfiguration' here
    }

    public int? MaxLength { get; private set; }

    public new StringPropertyConfiguration HasMaxLength(Nullable<int> value)
    {
        MaxLength = value;
        return base.HasMaxLength(value);
    }
}
I'm trying to show proof of concept by focusing first on the string property's max length constraint, but I can't get at that either, because of the internal constructor (Intellisense shown in constructor body comment).

This is far from working, but I'd appreciate any sage wisdom before I jump down this rabbit hole. Is what I'm asking for possible somehow? Am I on the right track? I'm willing to do the work, but I just need a little guidance. Thanks!

Viewing all articles
Browse latest Browse all 1793


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>