Consider a simple microblogging platform with the data model defined as follows:
[Table("users")]
public class User
{
[Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Column("name"), MaxLength(45), Index(IsUnique = true)]
public string Name { get; set; }
[Column("password"), MaxLength(45)]
public string Password { get; set; }
public virtual ICollection<Message> Messages { get; set; }
}
[Table("messages")]
public class Message
{
[Column("id"), Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Column("text"), MaxLength(512)]
public string Text { get; set; }
[Column("author")]
public virtual User Author { get; set; }
}
When I were designing it the database (SQL DDL) first way, I have included the messages.author
referencing users.id
as a foreign key. I like it this way: tidy and explanatory.
When I have tried the code-first way, however, I have found that it names the foreign key column messages.User_Id
rather than messages.author
despite the fact I have specified the [Column("author")]
attribute explicitly.
How can this behaviour be possibly corrected?
I would agree to name it simply .user
(instead of my original .author
idea) to make the reference more obvious if necessary, but certainly not .User_Id
- I totally dislike it when this obvious, redundant and ugly _Id
gets appended to key columns.
I use an SQLite database with the SQLiteCodeFirst library and am not sure whether this is normal Entity Framework behaviour or just a bug in this particular library.