Alex Rivera | Logout

Code First Entity Framework adds an underscore to a primary key column name

Asked 2011-12-20T09:49:04.277
9

I have a fluent mapping of a domain class that defines the names for each column including the primary key which is made up of two columns, NotificationId and IdentityId. These are also foreign keys that point at Notification.Id and Identity.Id respectively. Whenever I use this mapping as part of a query it generates a sql query with an underscore in between Notification and Id (Notification_Id) that is not mentioned anywhere in my mappings.

I would expect that there might be some convention that says that primary keys or foreign keys should look like that but it seems odd given that I've explicitly told it what the column name for NotificationId is.

Any help would be appreciated.

Added mapping file

public class Notifications_IdentitiesMap : EntityTypeConfiguration<Notifications_Identities>
{
    public Notifications_IdentitiesMap()
    {
        ToTable("Notifications.Notifications_Identities");
        HasKey(x => new { x.NotificationId,x.IdentityId });
        Property(x => x.IdentityId).HasColumnName("IdentityId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        Property(x => x.NotificationId).HasColumnName("NotificationId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        Property(x => x.SendAttempts).HasColumnName("SendAttempts");
        Property(x => x.IsSent).HasColumnName("IsSent");
        Property(x => x.LastSendAttempt).HasColumnName("LastSendAttempt");
        HasRequired(x => x.Notification).WithMany().HasForeignKey(x => x.NotificationId);
        HasRequired(x => x.Identity).WithMany().HasForeignKey(x => x.IdentityId);
    }
}
public class Notifications_Identities
{

    public Notifications_Identities()
    {

    }
    public Notifications_Identities(Notification notification, int identityId)
    {
        Notification = notification;
        IdentityId = identityId;
    }
    public virtual int IdentityId { get; set; }
    public virtua
Edit
Report

1 Answer

11

I made a stupid mistake, found the answer in Entity Framework 4.1 Code First Foreign Key Id's

I added the following:

public virtual ICollection<Notifications_Identities> Identities { get; set; }  

to the Notification entity and didn't map it

The fix was to change

HasRequired(x => x.Notification).WithMany().HasForeignKey(x => x.NotificationId);

to

HasRequired(x => x.Notification).WithMany(x=>x.Identities).HasForeignKey(x => x.NotificationId);
answered 2011-12-20T11:47:02.960

Your Answer