richardlawley / entityframeworkattributeconfig Goto Github PK
View Code? Open in Web Editor NEWConfigure code-first mappings using attributes
License: MIT License
Configure code-first mappings using attributes
License: MIT License
Could you designate a license for this project?
The namespace System.Data.Entity.ModelConfiguration.Conventions
doesn't exist in .Net Core.
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);
}
}
}
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?
How Can i Add Error Message to CDecimalPrecisionAttribute?!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.