PostSharp and EntityFramework CodeFirst Migrations

dom's Avatar

dom

28 Nov, 2017 11:35 AM

Hi
Consider the following class

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

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

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

        [PostSharp.Patterns.Contracts.Required]
        public decimal Quantity { get; set; }

        public decimal UnitPrice { get; set; }

        [PostSharp.Patterns.Contracts.Required]
        [StringLength(3)]
        public string SpeciesCode { get; set; }

        [PostSharp.Patterns.Contracts.Required]
        [StringLength(2)]
        public string FreshnessCode { get; set; }

        [PostSharp.Patterns.Contracts.Required]
        [StringLength(3)]
        public string StateCode { get; set; }

        [Required]
        [StringLength(3)]
        public string PresentationCode { get; set; }

        [PostSharp.Patterns.Contracts.Required]
        [StringLength(1)]
        public string SizeCode { get; set; }

        [PostSharp.Patterns.Contracts.Required]
        public string DefaultFaoArea { get; set; }

        [PostSharp.Patterns.Contracts.Required]
        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()
        {
            CreateTable(
                "Landings.LandingDetails",
                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

Dom

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

    PostSharp Technologies's Avatar

    Hello,

    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 http://doc.postsharp.net/contracts.

    If you'd like to have this feature in PostSharp, you can, however, suggest it at https://postsharp.uservoice.com/.

    Best regards,
    -tony

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

    PostSharp Technologies's Avatar

    Hello,

    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.

    Thanks,
    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

Generic

? 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