Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity annotation attributes do not work

Here is my entity:

[Table( Name = "PdfMeta" )]
public class Meta
{
    [Key()]
    public int Id { get; set; }

    [Column(Name = "TotalPages")]
    public int TotalPages { get; set; }

    [Column(Name = "PdfPath")]
    public string PdfUri { get; set; }

    [Column(Name = "ImagePath")]
    public string ImageUri { get; set; }

    [Column(Name = "SplittedPdfPath")]
    public string SplittedFolderUri { get; set; }

}

Here is code from context:

      public DbSet<Meta> PdfMeta { get; set; }

Why new table (Metas) has created with ImageUri, PdfUri ... columns? I know that this was done by convention but I have explisitly specifyed table and columns.

like image 303
NET Avatar asked Mar 15 '13 16:03

NET


People also ask

When to use NotMapped attribute?

The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database. In the following example, the AuditLog class will not be mapped to a table in the database: public class Contact.

Does EF core support data annotation attributes?

Configuration enables you to override EF Core's default behaviour. Configuration can be applied in two ways, using the Fluent API, and through DataAnnotation attributes. Attributes are a kind of tag that you can place on a class or property to specify metadata about that class or property.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.


1 Answers

Name property of ColumnAttribute has only getter defined. Pass column name in constructor instead:

[Table("PdfMeta")]
public class Meta
{
    [Key]
    public int Id { get; set; }

    [Column("TotalPages")]
    public int TotalPages { get; set; }

    [Column("PdfPath")]
    public string PdfUri { get; set; }

    [Column("ImagePath")]
    public string ImageUri { get; set; }

    [Column("SplittedPdfPath")]
    public string SplittedFolderUri { get; set; }
}

BTW ColumnAttribute defined in EntityFramework.dll. Looks like you have referenced ColumnAttribute from System.Data.Linq.dll

like image 51
Sergey Berezovskiy Avatar answered Oct 30 '22 18:10

Sergey Berezovskiy