PostSharp and EntityFramework CodeFirst Migrations

dom's Avatar


28 Nov, 2017 11:35 AM

Consider the following class

using PostSharp.Patterns.Model;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Landings
    [Table("LandingDetails", Schema = "Landings")]
    public class LandingDetail
        [Column(Order = 1)]
        public int LandingHeaderId { get; set; }

        [Column(Order = 2)]
        public int ProductId { get; set; }

        public decimal Quantity { get; set; }

        public decimal UnitPrice { get; set; }

        public string SpeciesCode { get; set; }

        public string FreshnessCode { get; set; }

        public string StateCode { get; set; }

        public string PresentationCode { get; set; }

        public string SizeCode { get; set; }

        public string DefaultFaoArea { get; set; }

        public string DefaultFaoSubArea { get; set; }

        public decimal LiveWeightConversionFactor { get; set; }


You'll notice that in the main I am using PostSharp attributes as Attributes.

Now consider the table structure that is inferred from this class when using EF CodeFirst add-migration.

    public override void Up()
                c => new
                        LandingHeaderId = c.Int(nullable: false),
                        ProductId = c.Int(nullable: false),
                        Quantity = c.Decimal(nullable: false, precision: 18, scale: 2),
                        UnitPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
                        SpeciesCode = c.String(maxLength: 3),
                        FreshnessCode = c.String(maxLength: 2),
                        StateCode = c.String(maxLength: 3),
                        PresentationCode = c.String(nullable: false, maxLength: 3),
                        SizeCode = c.String(maxLength: 1),
                        DefaultFaoArea = c.String(),
                        DefaultFaoSubArea = c.String(),
                        LiveWeightConversionFactor = c.Decimal(nullable: false, precision: 18, scale: 2),
                .PrimaryKey(t => new { t.LandingHeaderId, t.ProductId })
                .ForeignKey("Landings.LandingHeaders", t => t.LandingHeaderId, cascadeDelete: true)
                .Index(t => t.LandingHeaderId);

Notice that the strings using the PostSharp Required Contract are not marked as having Nullable:False whereas those using the standard system.componentmodel.dataannotations are. The PostSharp contract for string length however is respected. Is there a reason for this apparent anomaly or is something going on that shouldn't be. Prior to EF add-migration actually creating the class that would define the table to be created it build the project in which the LandingDetail class is defined so I'm guessing that it should by then have the PS contract defined in code...or have I missed something?

Many thanks


  1. Support Staff 1 Posted by PostSharp Techn... on 29 Nov, 2017 11:36 AM

    PostSharp Technologies's Avatar


    the StringLengthAttribute attribute in your code is the one from System.ComponentModel.DataAnnotations namespace, not the one from PostSharp.

    PostSharp code contracts are used for run-time precondition checking, not for EF CF model annotation. See

    If you'd like to have this feature in PostSharp, you can, however, suggest it at

    Best regards,

  2. Support Staff 2 Posted by PostSharp Techn... on 13 Dec, 2017 02:47 PM

    PostSharp Technologies's Avatar


    We are going to close this request as there have not been any further updates. Please feel free to reopen the discussion if you need more help.

    PostSharp Team

  3. PostSharp Technologies closed this discussion on 13 Dec, 2017 02:47 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac