更改EF生成的列名称

时间:2015-05-27 12:13:40

标签: c# asp.net sql-server entity-framework

我有两种模式:

[Table("aspnet_Users")]
public class User
{
    [Key]
    public Guid UserID { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<UserLog> UserLogs { get; set; }
}

[Table("UsersLog")]
public class UserLog
{
    [Key]
    public int ID { get; set; }
    public DateTime DateTime { get; set; }

    public virtual User User { get; set; }
}

我想从名为UserID的列中获取UserID。我试过这个:

modelBuilder.Entity<UserAll>().HasMany(u => u.UserLogs).WithOptional().Map(m => m.MapKey("UserID"));

来自问题:How to specify a column Name for EF5 navigation property,但它会生成:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[DateTime] AS [DateTime], 
[Extent1].[UserID] AS [UserID], 
[Extent1].[User_UserID] AS [User_UserID]
FROM [dbo].[UsersLog] AS [Extent1]

并抛出异常,因为我的数据库中没有User_UserID列。如何修复?

0 个答案:

没有答案