Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实体框架6.1-生成的SQL具有不正确的列名_Sql_Asp.net_Vb.net_Entity Framework_Webforms - Fatal编程技术网

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

实体框架6.1-生成的SQL具有不正确的列名,sql,asp.net,vb.net,entity-framework,webforms,Sql,Asp.net,Vb.net,Entity Framework,Webforms,EF 6.1生成了错误的查询sql,因此没有返回我的数据 因此,EF正在生成以下SQL,其中粗体显示了Employee_EmployeeNo的不正确列名,该列名实际上应该是EmployeeId exec sp_executesql N'SELECT [Limit1].[C1] AS [C1], [Limit1].[Username] AS [Username], [Limit1].[EmployeeCode] AS [EmployeeCode], [Lim

EF 6.1生成了错误的查询sql,因此没有返回我的数据

因此,EF正在生成以下SQL,其中粗体显示了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
部分公共类员工
_
_
公共属性EmployeeNo作为字符串
_
_
作为字符串的公共属性名
_
_
公共财产姓氏为字符串
_
公共属性StartDate为DateTime
公共属性EndDate是否为DateTime?
_
公共属性SiteId为整数
作为站点的公共可重写属性站点
作为用户的公共可重写属性用户
末级
使用者

部分公共类用户
_
_
公共属性用户名作为字符串
_
_
公共属性EmployeeNo作为字符串
公共属性以布尔形式活动
_
公共属性CreatedDate作为日期时间
公共属性是否修改为日期时间?
_
公共属性LastMachineUsed为字符串
公共财产LastLogonDate作为日期时间?
公共可重写属性Employee As Employee
公共可重写属性UserSecurity作为UserSecurity
末级
用户安全

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
部分公共类用户安全
_
_
公共属性用户名作为字符串
_
_
作为字符串的公共属性ObjectName
公共属性IsSecurityGroup为布尔值
_
_
公共属性AccessLevel为字符串
_
公共属性检查顺序为整数
公共属性CreatedDate作为日期时间
作为用户的公共可重写属性用户
末级
我错在哪里? 为什么EF会忽略Column属性? 为什么在SQL中使用ClassName\u KeyName而不是Column属性中的字段名

干杯

应该是

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

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

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


您可以在类中引入外键属性(如果希望调用列而不是属性名,则使用注释)。或者,您始终可以重命名引入的FK,而不将其包含在您的模型中-。

但是我将列名指定为“EmployeeId”
\uuuu公共属性EmployeeNo as String
我一辈子都想不出如何在这个回复中使用格式化的代码段,lolI发现它不是:
返回context.Users.Find(Username)
我可以使用:
返回context.Users.SqlQuery(“选择u.Username、u.EmployeeCode、u.IsActive、u.CreatedDate、u.ModifiedDate、u.LastMachineUsed、u.LastLogonDate、u.EmployeeCode、u.ObjectName FROM tblUser作为u.Username=us上的左外部连接tblUserSecurity作为us.Username=us.Username其中(u.Username=@Username)”,新的SqlParameter(“@Username”,Username)).SingleOrDefault
但这肯定会挫败使用类似EF的ORM的目标??嗨,Rowan,我已经尝试了你的建议,但似乎没有对其进行排序。modelBuilder.Entity(of Employee)(of Employee)(uu.HasOptional(Function(e)e.User)u.WithRequired(Function(e)e.Employee)u.Map(函数(e)e.MapKey(“EmployeeCode”)u.WillCascadeOnDelete(False)我希望这会产生正确的影响,但我得到以下错误。EmployeeCode:Name:类型中的每个属性名称必须是唯一的。属性名称“EmployeeCode”已经定义。
modelBuilder.Entity(Of Employee)() _
.Property(Function(e) e.EmployeeNo) _
.IsUnicode(False)
modelBuilder.Entity(Of Employee)() _
.Property(Function(e) e.EmployeeId) _
.IsUnicode(False)