GithubHelp home page GithubHelp logo

richardlawley / entityframeworkattributeconfig Goto Github PK

View Code? Open in Web Editor NEW
14.0 14.0 3.0 488 KB

Configure code-first mappings using attributes

License: MIT License

Batchfile 0.90% PowerShell 70.91% C# 24.21% XSLT 3.98%

entityframeworkattributeconfig's People

Contributors

richardlawley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

entityframeworkattributeconfig's Issues

License?

Could you designate a license for this project?

String is Unicode Attribute

Here's another attribute you can add to this library:

/// <summary>Attribute for string is unicode. This class cannot be inherited.</summary>
/// <seealso cref="T:System.Attribute"/>
[AttributeUsage(AttributeTargets.Property)]
public sealed class StringIsUnicodeAttribute : Attribute
{
    /// <summary>Initializes a new instance of the <see cref="StringIsUnicodeAttribute"/> class.</summary>
    /// <param name="isUnicode">true if this object is unicode, false if not.</param>
    public StringIsUnicodeAttribute(bool isUnicode)
    {
        IsUnicode = isUnicode;
    }

    /// <summary>Gets a value indicating whether this object is unicode.</summary>
    /// <value>true if this object is unicode, false if not.</value>
    public bool IsUnicode { get; }
}

And here's how I'm implementing it with my modelBuilder, you would need to convert this to your Convention methodology:

private static void ImplementStringIsUnicodeAttributes(DbModelBuilder modelBuilder)
{
    // Implement String Is Unicode Attributes
    foreach (var classType in Assembly.GetAssembly(typeof(StringIsUnicodeAttribute))
                                .GetTypes()
                                .Where(t => t.IsClass && t.Namespace == "YOUR.NAMESPACE"))
    {
        foreach (var propAttr in classType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                    .Where(p => p.GetCustomAttribute<StringIsUnicodeAttribute>() != null)
                                    .Select(p => new { prop = p, attr = p.GetCustomAttribute<StringIsUnicodeAttribute>(true) }))
        {
            var entityConfig = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(classType).Invoke(modelBuilder, null);
            var param = Expression.Parameter(classType, "c");
            var property = Expression.Property(param, propAttr.prop.Name);
            var lambdaExpression = Expression.Lambda(property, true, param);
            var methodInfo = entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[4];
            var stringConfig = methodInfo.Invoke(entityConfig, new object[] { lambdaExpression }) as StringPropertyConfiguration;
            stringConfig?.IsUnicode(propAttr.attr.IsUnicode);
        }
    }
}

Use a Convention insteat of Reflection

Hi Richard,

first off all: This is a very great Library!

After some troubles with inheritance and generic types, i have found a nice way to implement this:

using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration.Conventions;

public class DecimalPrecisionAttributeConvention : PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>
{
    public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)
    {
        configuration.HasPrecision(attribute.Precision, attribute.Scale);
    }
}

and then in the OnModelCreation:

modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());

What do you think about this solution?

Error Message

How Can i Add Error Message to CDecimalPrecisionAttribute?!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.