C# 重命名代码优先EF类中的外键属性会导致异常

C# 重命名代码优先EF类中的外键属性会导致异常,c#,.net,entity-framework,entity-framework-6,C#,.net,Entity Framework,Entity Framework 6,我已经使用EF6生成了几个类(首先是现有SQL Server 2012数据库的代码)。我正在尝试重命名自动生成的属性,以遵守一些命名约定,例如从record\u number到number。然后使用ColumnAttribute提供映射 根据我的观察,只要我不重命名SQL Server数据库中的外键的属性,这种方法就可以工作。例如,在以下情况下,如果我重命名record\u store\u id或record\u pool\u id——两者都是FK列,则会引发异常: [Table("intern

我已经使用EF6生成了几个类(首先是现有SQL Server 2012数据库的代码)。我正在尝试重命名自动生成的属性,以遵守一些命名约定,例如从
record\u number
number
。然后使用
ColumnAttribute
提供映射

根据我的观察,只要我不重命名SQL Server数据库中的外键的属性,这种方法就可以工作。例如,在以下情况下,如果我重命名
record\u store\u id
record\u pool\u id
——两者都是FK列,则会引发异常:

[Table("internal_records")]
public partial class Record
{
    [Key]
    [Column("record_number", Order = 0)]
    public long Number { get; set; }

    [Key]
    [Column("collection_id", Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CollectionId { get; set; }

    public short record_store_id { get; set; } // Exception if I rename this to e.g. RecordStoreId and add Column("record_store_id")

    [Required]
    [MaxLength(255)]    
    public byte[] record_pool_id { get; set; } // Exception if I rename this to e.g. RecordStoreId and add Column("record_store_id")

    ... // some other properties (not problematic)

    public virtual Collection InternalCollection { get; set; }
    public virtual Pool InternalPool { get; set; }
}
即,如果我使用:

[Column("record_store_id")]
public short RecordStoreId { get; set; }

[Required]
[Column("record_pool_id")]
[MaxLength(255)]    
public byte[] RecordPoolId { get; set; }
使用Linq查询时将引发异常:

Unhandled Exception: System.InvalidOperationException: The properties expression 'e => new <>f__AnonymousType0`2(record_store_id = e.RecordStoreId, record_pool_id = e.RecordPoolId)' is not valid. The expression should represent a property: C#: 't => t.MyProperty'  VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }'  VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.
   at System.Data.Entity.ModelConfiguration.Configuration.DependentNavigationPropertyConfiguration`1.HasForeignKey[TKey](Expression`1 foreignKeyExpression)
未处理的异常:System.InvalidOperationException:属性表达式“e=>new f_uAnonymousType0`2(记录\u存储\u id=e.RecordStoreId,记录\u池\u id=e.RecordPoolId)”无效。表达式应该表示一个属性:C#::“t=>t.MyProperty”VB.Net:“Function(t)t.MyProperty”。指定多个属性时,请使用匿名类型:C#::“t=>new{t.MyProperty1,t.MyProperty2}”VB.Net:“函数(t)new With{t.MyProperty1,t.MyProperty2}”。
位于System.Data.Entity.ModelConfiguration.Configuration.DependentNavigationPropertyConfiguration`1.HasForeignKey[TKey](表达式`1 foreignKeyExpression)
我尝试过使用
ForeignKeyAttribute
,但我肯定做得不对,因为这并没有解决我的问题

record\u store\u id
internal\u stores
表(映射到
store
类)中的主键,
record\u pool\u id
internal\u pool
表(映射到
pool
类)中的主键


我如何才能让它工作?

结果表明,问题出在我重命名属性时,
OnModelCreating()
正在更新的过程中。最初,代码是:

modelBuilder.Entity<InternalPool>()
    .HasMany(e => e.internal_records)
    .WithRequired(e => e.InternalPool)
    .HasForeignKey(e => new { e.record_store_id, e.record_pool_id })
    .WillCascadeOnDelete(false);
我必须手动更改此项以反映我重命名的值,即对于
RecordStoreId
RecordPoolId

.HasForeignKey(e => new { e.RecordStoreId, e.RecordPoolId })
.HasForeignKey(e => new { e.RecordStoreId, e.RecordPoolId })