LINQ无法识别EF生成的实体成员吗?

时间:2019-07-29 05:51:53

标签: c# entity-framework-6 .net-4.0 linq-to-entities

var user = db.t_ST_User
    .Where(_user => string.Compare(domain, _user.domainName, StringComparison.OrdinalIgnoreCase) == 0)
    .Where(_user => string.Compare(samAccountName, _user.samAccountName, StringComparison.OrdinalIgnoreCase) == 0)
    .Where(_user => _user.deleted == false)
    .FirstOrDefault();

上面的代码给我以下错误:

  

LINQ to Entities不支持指定的类型成员'domainName'。仅支持初始化程序,实体成员和实体导航属性。

但是domainNamet_ST_User的实体成员吗?

代码在迁移到新计算机之前可以正常工作,不确定是否相关。

public static int GetUserId(string domainName)
{
    int nonExistingUserId = 0;
    try
    {
        string message = "";

        using (var db = new SurfaceTreatment.SurfaceTreatmentEntities())
        {
            string domain = domainName.Split('\\')[0].Trim();
            string samAccountName = domainName.Split('\\')[1].Trim();
            var user = db.t_ST_User
                        .Where(_user => string.Compare(domain, _user.domainName, StringComparison.OrdinalIgnoreCase) == 0)
                        .Where(_user => string.Compare(samAccountName, _user.samAccountName, StringComparison.OrdinalIgnoreCase) == 0)
                        .Where(_user => _user.deleted == false)
                        .FirstOrDefault();
            if (user == null)
            {
                message = "Failed to get user by domain name: " + domainName;
                LogHandler.LogDebug(logger, message);
                return nonExistingUserId;
            }
            else
            {
                return user.userId;
            }
        }
    }
    catch (Exception ex)
    {
        LogHandler.LogError(logger, ex);
        return nonExistingUserId;
    }
}

public partial class t_ST_User
{
    public int userId { get; set; }
    public string badgeNumber { get; set; }
    public string domainName { get; set; }
    public string samAccountName { get; set; }
    public string userName { get; set; }
    public string userRemark { get; set; }
    public System.DateTime createDate { get; set; }
    public int createBy { get; set; }
    public Nullable<System.DateTime> updateDate { get; set; }
    public Nullable<int> updateBy { get; set; }
    public bool deleted { get; set; }
    public Nullable<System.DateTime> deleteDate { get; set; }
    public Nullable<int> deleteBy { get; set; }
    public string email { get; set; }
    public bool confirmBeforeSubmitToApprover { get; set; }
    public string preferredCompany { get; set; }
    public string preferredDepartment { get; set; }
    public string preferredPlant { get; set; }
    public Nullable<int> preferredBuildingAddressId { get; set; }
}

创建表脚本:

CREATE TABLE [dbo].[t_ST_User](
    [userId] [int] IDENTITY(1,1) NOT NULL,
    [badgeNumber] [nvarchar](50) NOT NULL,
    [domainName] [nvarchar](50) NOT NULL,
    [samAccountName] [nvarchar](50) NOT NULL,
    [userName] [nvarchar](150) NOT NULL,
    [userRemark] [nvarchar](max) NULL,
    [createDate] [datetime] NOT NULL,
    [createBy] [int] NOT NULL,
    [updateDate] [datetime] NULL,
    [updateBy] [int] NULL,
    [deleted] [bit] NOT NULL,
    [deleteDate] [datetime] NULL,
    [deleteBy] [int] NULL,
    [email] [nvarchar](max) NULL,
    [confirmBeforeSubmitToApprover] [bit] NOT NULL,
    [preferredCompany] [nvarchar](50) NULL,
    [preferredDepartment] [nvarchar](50) NULL,
    [preferredPlant] [nvarchar](50) NULL,
    [preferredBuildingAddressId] [int] NULL,
 CONSTRAINT [PK_t_ST_User] PRIMARY KEY CLUSTERED 
(
    [userId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

1 个答案:

答案 0 :(得分:0)

从edmx中删除t_ST_User,然后再次通过Update Model from Database重新添加后,问题已解决。