none
How can I implement/mapp an state pattern in entity 4.1 Code First? RRS feed

  • Question

  • I'm trying mapp an entity using Entity Framwork 4.1, Code First and Fluent API. My scenary is that I'm implementing an State Pattern using an abstract class.
    See my implementation:


    //my contract

        public abstract class SaleStatus
        {
                public int IdSaleStatus { get; set; }
                public string Description { get; set; }
               
                public abstract void ChangeTo(Sale sale, SaleStatus saleStatus);
        }

        public class SaleStatusBooked : SaleStatus
        {
                public SaleStatusBooked()
                {
                    this.IdSaleStatus = 1;
                    Description = "Reservado";
                }
             
                public override void ChangeTo(Sale sale, SaleStatus newStatus)
                {
                    if (!(newStatus is SaleStatusPaid) || (!(newStatus is SaleStatusTimeOut)))
                        throw new BusinessException("salestatus");
       
                    sale.SetStatus(newStatus);
                }
        }
       
         public class SaleStatusPaid : SaleStatus
            {
                public SaleStatusPaid()
                {
                    this.IdSaleStatus = 2;
                    Description = "Pago";
                }
            
                public override void ChangeTo(Sale sale, SaleStatus newStatus)
                {
                    if (!(newStatus is SaleStatusCancelled) || (!(newStatus is SaleStatusNotResolved)) || (!(newStatus is SaleStatusCriticalError)))
                        throw new BusinessException("salestatus");
       
                    sale.SetStatus(newStatus);
                }
            }

    And Now, my current mapping of SaleStatus:

        public class SaleStatusMap: EntityTypeConfiguration<SaleStatus>
            {
                public SaleStatusMap()
                {
                    ToTable("SaleStatus");
                    HasKey(ss => ss.IdSaleStatus);
       
                    Map<SaleStatusBooked>(pk => pk.Requires("IdSaleStatus").HasValue(1));               
                    Map<SaleStatusPaid>(pk => pk.Requires("IdSaleStatus").HasValue(2));        
                }
            }

    When I run my project I received this message:

    "Schema specified is not valid. Errors:
    (107,6) : error 0075: Key Part: 'IdSaleStatus' for type SaleStatus is not valid. All parts of the key must be non nullable."


    Anyone can help me?


    Thanks,

    Best Regards.

    Michel Magalhães




    Saturday, August 27, 2011 8:28 PM

Answers

  • Hi, 

    as I see you are using CodeFirst inheritance mapping with Table per Hierarchy approach; in this case OnModelCreated method should look like this:

     

     

       17         protected override void OnModelCreating(DbModelBuilder modelBuilder)

       18         {

       19             modelBuilder.Entity<SaleStatus>().HasKey(s => s.SaleStatusID)

       20                 .Map<SaleStatusBooked>(m => m.Requires("SaleStatusType").HasValue(1))

       21                 .Map<SaleStatusPaid>(m => m.Requires("SaleStatusType").HasValue(2));

       22             base.OnModelCreating(modelBuilder);

       23         }

     

     

    or, your SaleStatusMap class - like this:

     

     

     

       26     public class SaleStatusMap : EntityTypeConfiguration<SaleStatus>

       27     {

       28         public SaleStatusMap()

       29         {

       30             ToTable("SaleStatuses");

       31             HasKey(s => s.SaleStatusID);

       32             Map<SaleStatusBooked>(m => m.Requires("SaleStatusType").HasValue(1));

       33             Map<SaleStatusPaid>(m => m.Requires("SaleStatusType").HasValue(2));

       34         }

       35     }

     

     

     

    primary key (SaleStatusID) and discriminator (SaleStatusType) are different things, this might help you also:

     

    http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx

     

     

    Regards

     

     

     


    Clarity VS Precision
    • Marked as answer by Larcolais Gong Sunday, September 4, 2011 2:23 AM
    Sunday, August 28, 2011 8:26 AM