比较实体框架3.5中的外键
我有一个具有以下结构的组织表,
[dbo].[Organizations](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Phone] [nvarchar](13) NULL,
[Fax] [nchar](11) NULL,
[Address] [nvarchar](100) NULL,
[URL] [varchar](50) NULL,
[Email] [nvarchar](50) NULL,
[EstablishedYear] [nchar](4) NULL,
[CategoryId] [int] NULL,
[RegionId] [int] NULL,
[CityId] [int] NULL,
[ProvinceId] [int] NULL,
[CountryId] [int] NULL,
[ImageFileName] [nvarchar](50) NULL)
因为我使用的是实体框架 3.5, 我使用了一个分部类来添加外键属性(对于countryid,provinceid,...)
public partial class Organization
{
public int? CountryId
{
get
{
if (CountryReference.EntityKey == null)
return null;
return (int)CountryReference.EntityKey.EntityKeyValues[0].Value;
}
set
{
if (value != null && value != -1)
CountryReference.EntityKey = new EntityKey("Entities.Countries", "CountryId", value);
else
CountryReference.EntityKey = null;
}
}
}
现在我有一个查询,但它抛出一个异常:
查询:
if (Enumerable.Any(ctx.Organizations.Where(s => s.CountryId== Organization.CountryId && s.ProvinceId == Organization.ProvinceId && s.CityId == Organization.CityId && s.Name == Organization.Name)))
异常:
The specified type member 'CountryId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
我只想比较导航属性,有什么想法吗?
I have an organization table with following structure
[dbo].[Organizations](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Phone] [nvarchar](13) NULL,
[Fax] [nchar](11) NULL,
[Address] [nvarchar](100) NULL,
[URL] [varchar](50) NULL,
[Email] [nvarchar](50) NULL,
[EstablishedYear] [nchar](4) NULL,
[CategoryId] [int] NULL,
[RegionId] [int] NULL,
[CityId] [int] NULL,
[ProvinceId] [int] NULL,
[CountryId] [int] NULL,
[ImageFileName] [nvarchar](50) NULL)
because I'm using entity framework 3.5,
I've used a partial class to add foreign key properties (for countryid, provinceid ,...)
public partial class Organization
{
public int? CountryId
{
get
{
if (CountryReference.EntityKey == null)
return null;
return (int)CountryReference.EntityKey.EntityKeyValues[0].Value;
}
set
{
if (value != null && value != -1)
CountryReference.EntityKey = new EntityKey("Entities.Countries", "CountryId", value);
else
CountryReference.EntityKey = null;
}
}
}
Now I have a query but it throws an exception:
Query:
if (Enumerable.Any(ctx.Organizations.Where(s => s.CountryId== Organization.CountryId && s.ProvinceId == Organization.ProvinceId && s.CityId == Organization.CityId && s.Name == Organization.Name)))
Exception:
The specified type member 'CountryId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
I just want to compare navigation properties, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在 linq-to-entities 查询中使用分部类中定义的属性。您必须直接使用导航属性:
You can't use properties defined in partial class in linq-to-entities query. You must use navigation property directly:
我找到了解决方案:
在使用 where 条件之前,我使用了 AsEnumerable() 方法。
i found the solution:
befor using where condition i used AsEnumerable() method.