0

How can I add annotations to the partial class.

In Partial Class:

//[MetadataType(typeof(Table.Invoice_Details))]
    [MetadataTypeAttribute(typeof(Table.Invoice_Details))]
    public partial class Invoice_Details
    {
        [Display(Name = "Quantity")]
        public int Quantity { get; set; }
    }

In EntityFrameWork

    public partial class Invoice_Details
    {

        public int Quantity { get; set; }
    }

I tried in more than one way, but in the end I get a failure message:

Error CS0102 The type 'Invoice_Details' already contains a definition for 'Quantity'

  • 2
    Possible duplicate? Is your question the same as [this one](https://stackoverflow.com/q/3782405/3791245)? – Sean Skelly Jul 15 '21 at 22:31
  • 1
    Does this answer your question? [Can I define properties in partial classes, then mark them with attributes in another partial class?](https://stackoverflow.com/questions/3782405/can-i-define-properties-in-partial-classes-then-mark-them-with-attributes-in-an) (courtesy of Sean, ty) –  Jul 15 '21 at 23:00

1 Answers1

0

You cannot define a variable twice. You need to define it in another class and define the new class as part of the class you want with metadatatype.

main class

 public partial class Invoice_Details
 {    
    public int Quantity { get; set; }
 }

metadata class

namespace classnamespace
{
   public class InvoiceDetailsMetaData
   {
      [Display(Name = "Quantity")]
      public int Quantity { get; set; }
   }
   [MetadataType(typeof(classnamespace.InvoiceDetailsMetaData))]
   public partial class Invoice_Details 
   {
           
   }
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • 1
    Though a nice answer in the scheme of things uou should have flagged as duplicate rather than post an answer –  Jul 15 '21 at 23:10