Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 virtual int NotificationId { get; set; }
    public virtual int SendAttempts { get; set; }
    public virtual DateTime? LastSendAttempt { get; set; }
    public virtual Identities.Identity Identity { get; set; }
    public virtual Notification Notification { get; set; }
    public bool IsSent { get; set; }
}
public class NotificationMap:EntityTypeConfiguration<Notification>
{
    public NotificationMap()
    {
        ToTable("Notifications.Notifications");
        Property(x => x.Id).HasColumnName("Id");
        Property(x => x.Subject).HasColumnName("Subject").HasMaxLength(255);
        Property(x => x.Message).HasColumnName("Message");
        Property(x => x.TypeId).HasColumnName("TypeId");
        Property(x => x.DateCreated).HasColumnName("DateCreated");
        Property(x => x.CreatorIdentityId).HasColumnName("CreatorIdentityId");

        HasRequired(x => x.Creator).WithMany().HasForeignKey(x => x.CreatorIdentityId);
    }
}
public class IdentityMap : EntityTypeConfiguration<RM.Domain.Identities.Identity>
    {
        public IdentityMap()
        {
            Property(x => x.Id).HasColumnName("IDENTITYID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(x => x.FirstName).HasColumnName("firstname");
            Property(x => x.Surname).HasColumnName("surname");
            Property(x => x.Username).HasColumnName("username");
            Property(x => x.IsGroup).HasColumnName("is_group");
            Property(x => x.EmailAddress).HasColumnName("email");
            Property(x => x.ActiveDirectoryId).HasColumnName("ActiveDirectoryId");
            Property(x => x.IsLive).HasColumnName("is_active");
            ToTable("dbo.rm_tbl_IDENTITY_Identities");
        }
    }
like image 804
Eva Lacy Avatar asked Dec 20 '11 09:12

Eva Lacy


1 Answers

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);
like image 68
Eva Lacy Avatar answered Oct 19 '22 07:10

Eva Lacy