LINQ 不识别 EF 生成的实体成员?
LINQ do not recognize entity member generated by EF?
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();
上面的代码给我以下错误:
The specified type member 'domainName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
但是domainName
是t_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; }
}
创建table脚本:
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]
我从 edmx 中删除 t_ST_User
后问题已解决,然后再次通过 Update Model from Database
将其添加回来。
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();
上面的代码给我以下错误:
The specified type member 'domainName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
但是domainName
是t_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; }
}
创建table脚本:
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]
我从 edmx 中删除 t_ST_User
后问题已解决,然后再次通过 Update Model from Database
将其添加回来。