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

New Post: ModelBuilder and extended MetadataProperties (DataModelAnnotation?)

$
0
0
It has been a long a time since then. I checked the recent source code and implementing this functionality become much easier. The same logic looks like this now:
public abstract class ConfigurationBase
{
    // for now, only string annotations
    private Dictionary<string, string> annotations;

    public ConfigurationBase()
    {
        this.annotations = new Dictionary<string, string>();
    }

    public void AddAnnotation(string name, string value)
    {
        this.annotations[name] = value;
    }

    internal virtual void ConfigureMetadata(MetadataItem item)
    {
        item.AddMetadataProperties(annotations
            .Select(x =>
                new MetadataProperty(
                    x.Key,
                    TypeUsage.CreateStringTypeUsage(...),
                    x.Value))
            .ToList());

    }
}
public class PrimitivePropertyConfiguration<TConfiguration>
    where TConfiguration : PrimitivePropertyConfiguration
{
    public PrimitivePropertyConfiguration<TConfiguration> Annotate(
         string attributeNamespace, 
         string attributeName, 
         string value)
    {
        Configuration.AddAnnotation(
            string.Format("{0}:{1}", attributeNamespace, attributeName), 
            value);
        return this;
    }
}
The tricky part is to determine in which methods should the ConfigureMetadata method be called. For properties it was easy. Doing the same with entity types / tables seem to be harder (failed to do SS space in few minutes).
CS space:
PropertyConfiguration.Configure(EdmProperty property)

SS space:
PropertyConfiguration.Configure(
            EdmProperty column, EntityType table, DbProviderManifest providerManifest,
            bool allowOverride = false, 
            bool fillFromExistingConfiguration = false)
These few changes makes possible to annotate properties like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<Person>()
        .Property(x => x.Name)
        .Annotate("http://example.org/2012/property", "PropertyAnnotation", "value");
}
Is there any plan to implement this? Or would the team accept this as a contribution?

Viewing all articles
Browse latest Browse all 1793

Trending Articles



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