Question:
I have a code in C# Entity Framework code First.
On one side I have Article
and on the other Tax
. An Article can have many Taxes and a Tax can be in many Articles. I show what I'm doing and the error it gives me:
public class Tax
{
public int Id { get; set; }
[Display(Name = "Nombre")]
[MaxLength(50, ErrorMessage = "The {0} field can not have more than {1} characters.")]
[Required(ErrorMessage = "The field {0} is mandatory.")]
public string Name { get; set; }
public decimal Value { get; set; }
public ICollection<Article> Articles { get; set; }
}
public class Article
{
public int Id { get; set; }
[Display(Name = "Nombre")]
[MaxLength(200, ErrorMessage = "The {0} field can not have more than {1} characters.")]
[Required(ErrorMessage = "The field {0} is mandatory.")]
public string Name { get; set; }
public ArticleType ArticleType { get; set; }
public ArticleSubType ArticleSubType { get; set; }
public ICollection<Tax> Taxes { get; set; }
}
When doing the migration I get the following error:
Unable to determine the relationship represented by navigation property 'Article.Taxes' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
can you help me please thank you very much
Answer:
In principle you must create an auxiliary table (Code First) that relates the Articles ( Article
) with the Taxes ( Tax
), that is precisely what the error is indicating.
The table would be for example ArticleTax
, and it would relate the two tables you already have by their primary key:
public class ArticleTax
{
[Key]
[Column(Order=1)]
public int IdArticulo { get; set; }
[Key]
[Column(Order=2)]
public int IdTax { get; set; }
[ForeignKey("Id")]
public Article Article { get; set; }
[ForeignKey("Id")]
public Tax Tax { get; set; }
}
Once you migrate, the Article
and Tax
tables will be related through the new ArticleTax
table.
CLARIFICATION : You may need to change the primary keys of the Article
and Tax
tables, so that EF correctly understands the relationship. It would be like this:
public class Tax
{
[Key]
public int IdTax { get; set; }
...
...
}
public class Article
{
[Key]
public int IdArticle { get; set; }
...
...
}
And the new ArticleTax
table would look like this:
public class ArticleTax
{
[Key]
[Column(Order=1)]
public int IdArticle { get; set; }
[Key]
[Column(Order=2)]
public int IdTax { get; set; }
[ForeignKey("IdArticle")]
public Article Article { get; set; }
[ForeignKey("IdTax")]
public Tax Tax { get; set; }
}
UPDATE : In the case of getting the error:
Entity type 'ArticleTax' has composite primary key defined with data annotations. To set composite primary key, use fluent API.
You can use the Fluent API to create the double primary key of the ArticleTax
table.
In your DbContext
, in the OnModelCreating()
method, create the composite key like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ArticleTax>()
.HasKey(c => new { c.IdArticle, c.IdTax });
}
NOTE: Of course remove the [Key][Column(Order=1)]
and [Key][Column(Order=2)]
attributes from the ArticleTax
table.