GithubHelp home page GithubHelp logo

hollisyao / entityframeworkattributeconfig Goto Github PK

View Code? Open in Web Editor NEW

This project forked from richardlawley/entityframeworkattributeconfig

0.0 1.0 0.0 487 KB

Configure code-first mappings using attributes

Batchfile 0.33% PowerShell 14.31% C# 85.36%

entityframeworkattributeconfig's Introduction

Entity Framework Attribute Config

Allows you to configure properties by placing attributes on the properties of the Entity, rather than using the fluent configuration method. For example...

public class MyEntity 
{
    [DateTimePrecision(0)]    // Column will be created as datetime(0)
    public DateTime ShortDate { get; set; }
    
    [DecimalPrecision(18, 5)] // Column will be created as decimal(18,5)
    public decimal PreciseNumber { get; set; }
}

Install from nuget:

PM> Install-Package EFAttributeConfig

For each type of precision attribute you wish to use, add a line to your OnModelCreating method:

public class TestContext : DbContext 
{
	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
	    base.OnModelCreating(modelBuilder);
	    
        // Add conventions for Precision Attributes
	    modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
        modelBuilder.Conventions.Add(new DateTimePrecisionAttributeConvention());
	}
}

The following attributes have been implemented so far:

  • DecimalPrecisionAttribute - change the precision of a decimal
  • DateTimePrecisionAttribute - change the precision of a datetime2 or datetimeoffset type

Adding further attributes is simple - take a look at the implementation of the existing attributes for an example!

This project is an extension of this StackOverflow answer. Thanks to @richi2666 for the suggestion of using conventions instead of Reflection.

entityframeworkattributeconfig's People

Contributors

richardlawley avatar

Watchers

James Cloos avatar

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.