0

I am using entity framework (for learning) to create the datatable; however, when I run the application, it creates the database column name as: AnaKategori_ANA_CATEGORY_ID.

Why is it doing that since I am specifically telling it to name the column as "ANA_CATEGORY_ID"?

Below is the class files content.

[Table("CATEGORY")]
public class Kategori
{
    [Key]
    public Guid CATEGORY_ID { get; set; }
    [MaxLength(100)]
    public string CATEGORY { get; set; }
    [MaxLength(100)]
    public string CATEGORY_TR { get; set; }
    [Column("ANA_CATEGORY_ID")]
    public virtual AnaKategori AnaKategori { get; set; }

}

I tried boindiil's answer prior to posting (which was to use ForeignKey Attribute and I got this error:

The ForeignKeyAttribute on property 'AnaKategori' on type 'BusinessListingMVC.Models.Kategori' is         
not valid. The foreign key name 'ANA_CATEGORY_ID' was not found on the dependent type
BusinessListingMVC.Models.Kategori'. 

The Name value should be a comma separated list of foreign key property names.

Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in
the code. 
gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50
Adem A.
  • 1
  • 1
  • When you use the ForeignKey attribute, you need to have two fields in your class, an int (`ParentCategoryId`) and also the virtual field. – Ameen Jan 31 '13 at 18:15

1 Answers1

0

I have found this link, which points out that this seems to be a bug. In this case you have to use the [ForeignKey] attribute to give it the desired name:

[Table("CATEGORY")]
public class Kategori
{
    [Key]
    public Guid CATEGORY_ID { get; set; }
    [MaxLength(100)]
    public string CATEGORY { get; set; }
    [MaxLength(100)]
    public string CATEGORY_TR { get; set; }
    [Column("ANA_CATEGORY_ID")]
    public Guid ANA_CATEGORY_ID { get; set; }
    [ForeignKey("ANA_CATEGORY_ID")]
    public virtual AnaKategori AnaKategori { get; set; }
}
boindiil
  • 5,805
  • 1
  • 28
  • 31