I'm always using Attributes to map the properties of my entities to their corresponding columns. Here's an example:
[Table("news_entries")]
public class News
{
[Key]
public int Id { get; set; }
[Column("d_date")]
public DateTime Date { get; set; }
[Column("m_text")]
public string Text { get; set; }
[Column("id_user")]
public int UserId { get; set; }
[ForeignKey("UserId")]
public User User { get; set; }
}
But I still don't know, how I could map a Many-to-Many relationship, where the table and column names doesn't match with the properties.
I know I could use the DbModelBuilder
in my DbContext
, but I don't want to do the mapping outside of my entity class. Is it somehow possible to map those relationships with Attributes as well?
No. There is no class for the junction table where you could apply your attributes. Once you use direct many to many mapping (where junction table is hidden behind navigation properties) you need to use fluent API.
Perhaps EF6 will allow this through custom conventions but my initial experience with handling foreign key names with convention wasn't successful so I guess junction table will be the same story.