First of all I need to say that I've checked other similar questions on stackoverflow, all of them used Fulent API or ... to do this.
I'm trying to have an extra field in my junction table in EntityFramework, but there are only PrimaryKeys.
Does anybody know how could I do it with DataAnnotation?
If you want to add an extra column(s) to the junction table, you don't have other choice that map that table as an entity and create two one to many relationships between it and the entities that are involved, for example:
public class A
{
public int Id{get;set;}
//...
public virtual ICollection<AB> ABs{get;set;}
}
public class B
{
public int Id{get;set;}
//...
public virtual ICollection<AB> ABs{get;set;}
}
public class AB
{
[Key,ForeignKey("A"),Column(Order=1)]
public int AId{get;set;}
[Key,ForeignKey("B"),Column(Order=2)]
public int BId{get;set;}
public virtual A A{get;set;}
public virtual B B{get;set;}
//Add here the extra column(s)
}