实体框架6.1 - 生成的SQL具有不正确的列名称

时间:2014-12-10 10:58:56

标签: sql asp.net vb.net entity-framework webforms

得到并发现EF 6.1生成错误的查询sql,因此没有返回我的数据。

因此EF生成以下SQL, BOLD 显示Employee_EmployeeNo的错误列名,其实际上应该是EmployeeId

exec sp_executesql N'SELECT 
    [Limit1].[C1] AS [C1], 
    [Limit1].[Username] AS [Username], 
    [Limit1].[EmployeeCode] AS [EmployeeCode], 
    [Limit1].[IsActive] AS [IsActive], 
    [Limit1].[CreatedDate] AS [CreatedDate], 
    [Limit1].[ModifiedDate] AS [ModifiedDate], 
    [Limit1].[LastMachineUsed] AS [LastMachineUsed], 
    [Limit1].[LastLogonDate] AS [LastLogonDate], 
    [Limit1].[Employee_EmployeeNo] AS [Employee_EmployeeNo], 
    [Limit1].[Username1] AS [Username1], 
    [Limit1].[ObjectName] AS [ObjectName]
    FROM ( SELECT TOP (2) 
        [Extent1].[Username] AS [Username], 
        [Extent1].[EmployeeCode] AS [EmployeeCode], 
        [Extent1].[IsActive] AS [IsActive], 
        [Extent1].[CreatedDate] AS [CreatedDate], 
        [Extent1].[ModifiedDate] AS [ModifiedDate], 
        [Extent1].[LastMachineUsed] AS [LastMachineUsed], 
        [Extent1].[LastLogonDate] AS [LastLogonDate], 
        [Extent1].[**Employee_EmployeeNo**] AS [Employee_EmployeeNo], 
        [Extent2].[Username] AS [Username1], 
        [Extent2].[ObjectName] AS [ObjectName], 
        1 AS [C1]
        FROM  [dbo].[tblUser] AS [Extent1]
        LEFT OUTER JOIN [dbo].[tblUserSecurity] AS [Extent2] ON ([Extent2].[User_Username] IS NOT NULL) AND ([Extent1].[Username] = [Extent2].[User_Username])
        WHERE [Extent1].[Username] = @p0
    )  AS [Limit1]',N'@p0 varchar(8000)',@p0='USERNAME'

上下文

modelBuilder.Entity(Of Employee)() _
    .Property(Function(e) e.EmployeeNo) _
    .IsUnicode(False)

modelBuilder.Entity(Of Employee)() _
    .Property(Function(e) e.Forename) _
    .IsUnicode(False)

modelBuilder.Entity(Of Employee)() _
    .Property(Function(e) e.Surname) _
    .IsUnicode(False)

modelBuilder.Entity(Of Employee)() _
    .HasOptional(Function(e) e.User)



modelBuilder.Entity(Of User)() _
    .HasKey(Function(e) e.Username)

modelBuilder.Entity(Of User)() _
    .Property(Function(e) e.Username) _
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None) _
    .IsUnicode(False)

modelBuilder.Entity(Of User)() _
    .Property(Function(e) e.EmployeeNo) _
    .IsUnicode(False)

modelBuilder.Entity(Of User)() _
    .Property(Function(e) e.LastMachineUsed) _
    .IsUnicode(False)

modelBuilder.Entity(Of User)() _
    .Property(Function(e) e.LastMachineUsed) _
    .IsUnicode(False)

modelBuilder.Entity(Of User)() _
    .HasOptional(Function(e) e.Employee) _
    .WithOptionalDependent(Function(s) s.User)



modelBuilder.Entity(Of UserSecurity)() _
    .Property(Function(e) e.Username) _
    .IsUnicode(False)

modelBuilder.Entity(Of UserSecurity)() _
    .Property(Function(e) e.ObjectName) _
    .IsUnicode(False)

modelBuilder.Entity(Of UserSecurity)() _
    .Property(Function(e) e.AccessLevel) _
    .IsFixedLength() _
    .IsUnicode(False)

modelBuilder.Entity(Of UserSecurity)() _
    .HasOptional(Function(e) e.User) _
    .WithOptionalDependent(Function(e) e.UserSecurity)

员工

Partial Public Class Employee

    <Key> _
    <StringLength(10)> _
    <Column("EmployeeId")>
    Public Property EmployeeNo As String

    <Required> _
    <StringLength(50)> _
    <Column("Forename")>
    Public Property Forename As String

    <Required> _
    <StringLength(50)> _
    <Column("Surname")>
    Public Property Surname As String

    <Required> _
    <Column("StartDate", TypeName:="date")>
    Public Property StartDate As DateTime

    <Column("EndDate", TypeName:="date")>
    Public Property EndDate As DateTime?

    <Required> _
    <Column("SiteCode")>
    Public Property SiteId As Integer

    Public Overridable Property Site As Site

    Public Overridable Property User As User

End Class

用户

Partial Public Class User

    <Key> _
    <StringLength(50)> _
    <Column("Username")>
    Public Property Username As String

    <Required> _
    <StringLength(10)> _
    <Column("EmployeeCode")>
    Public Property EmployeeNo As String

    <Column("IsActive")>
    Public Property IsActive As Boolean

    <Required> _
    <Column("CreatedDate", TypeName:="date")>
    Public Property CreatedDate As DateTime

    <Column("ModifiedDate", TypeName:="date")>
    Public Property ModifiedDate As DateTime?

    <StringLength(50)> _
    <Column("LastMachineUsed")>
    Public Property LastMachineUsed As String

    <Column("LastLogonDate", TypeName:="date")>
    Public Property LastLogonDate As DateTime?

    Public Overridable Property Employee As Employee

    Public Overridable Property UserSecurity As UserSecurity

End Class

用户安全

Partial Public Class UserSecurity

    <Key> _
    <Column("Username", Order:=0)> _
    <StringLength(50)>
    Public Property Username As String

    <Key> _
    <Column("ObjectName", Order:=1)> _
    <StringLength(50)>
    Public Property ObjectName As String

    <Column("IsSecurityGroup")>
    Public Property IsSecurityGroup As Boolean

    <Required> _
    <StringLength(1)> _
    <Column("AccessLevel")>
    Public Property AccessLevel As String

    <DatabaseGenerated(DatabaseGeneratedOption.None)> _
    <Column("CheckOrder")>
    Public Property CheckOrder As Integer

    <Column("CreatedDate", TypeName:="date")>
    Public Property CreatedDate As DateTime

    Public Overridable Property User As User

End Class

我哪里错了? 为什么EF忽略列属性? 为什么在SQL属性中使用ClassName_KeyName而不是Column Attribute中的Field Name?

干杯

2 个答案:

答案 0 :(得分:0)

modelBuilder.Entity(Of Employee)() _
.Property(Function(e) e.EmployeeNo) _
.IsUnicode(False)

应该是

modelBuilder.Entity(Of Employee)() _
.Property(Function(e) e.EmployeeId) _
.IsUnicode(False)

答案 1 :(得分:0)

问题是您的User类中没有外键属性,因此EF引入了一个。按照惯例,它使用_作为外键列的名称。

我假设您的目标是现有数据库,否则EF会为您在数据库中生成此外键列。

您可以在类中引入外键属性(如果希望将列调用为属性名称以外的其他属性,则使用注释)。或者,您始终可以重命名引入的FK,而不将其包含在模型中http://msdn.microsoft.com/en-us/data/jj591620#IndependentAssociation