Search code examples
c#asp.netasp.net-mvcpartial-classes

Constructor in partial MetataData class


I have the following partial class, which provides Metadata to my Database first models.

namespace Model.Metadata.Routing
{
    [MetadataType(typeof(RoutingMetadata))]
    public partial class Routing
    {

    }

    public partial class RoutingMetadata
    {          
        [DefaultValue("%")]
        public string Slot { get; set; }

        [Required(ErrorMessage = "This field is requied")]
        [DefaultValue(0)]
        public int BlockStart { get; set; }
        [Required(ErrorMessage = "This field is requied")]
        [DefaultValue(499)]
        public int BlockEnd { get; set; }
        [DefaultValue(-1)]       
    }
}

Now I want to add a constructor for the Routing Class to default my values,

public Routing()
{
  Slot="%";
}

Where do I add the constructor?

[EDIT] Other half of partial Class Routing

 public partial class Routing
{      
    public string Slot { get; set; }
    public int BlockStart { get; set; }
    public int BlockEnd { get; set; }
}

Solution

  • You can put the constructor in any one of the partial class definitions, it's up to you where you think it makes the most logical sense.

    Of course, all parts of a partial class definition need to have the same class name and namespace, or they are different classes. Routing and RadioRouting are not the same class, because they don't have the same name.