Code First Entity Framework为主键列名添加下划线

时间:2011-12-20 09:49:04

标签: entity-framework ef-code-first

我有一个域类的流畅映射,它定义了每个列的名称,包括主键,它由两列NotifyId和IdentityId组成。这些也是分别指向Notification.Id和Identity.Id的外键。每当我将此映射用作查询的一部分时,它会生成一个sql查询,其中包含Notification和Id(Notification_Id)之间的下划线,这在我的映射中没有提到。

我希望可能会有一些惯例说主键或外键看起来应该是这样但是看起来很奇怪,因为我明确告诉它NotificationId的列名是什么。

任何帮助都将不胜感激。

添加了映射文件

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");
        }
    }

1 个答案:

答案 0 :(得分:11)

我犯了一个愚蠢的错误,在Entity Framework 4.1 Code First Foreign Key Id's

中找到答案

我添加了以下内容:

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

到Notification实体并且没有映射

解决方法是改变

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

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