比较实体框架3.5中的外键

发布于 2024-10-22 12:36:19 字数 1612 浏览 0 评论 0原文

我有一个具有以下结构的组织表,

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再见回来 2024-10-29 12:36:20

您不能在 linq-to-entities 查询中使用分部类中定义的属性。您必须直接使用导航属性:

ctx.Organizations.Where(o => o.Country.Id == someCountryId);

You can't use properties defined in partial class in linq-to-entities query. You must use navigation property directly:

ctx.Organizations.Where(o => o.Country.Id == someCountryId);
独守阴晴ぅ圆缺 2024-10-29 12:36:20

我找到了解决方案:

 if (Enumerable.Any(ctx.Organizations.AsEnumerable().
         Where(s => s.CountryId == Organization.CountryId && 
                    s.ProvinceId == Organization.ProvinceId && 
                    s.CityId == Organization.CityId && 
                    s.Name == Organization.Name)))

在使用 where 条件之前,我使用了 AsEnumerable() 方法。

i found the solution:

 if (Enumerable.Any(ctx.Organizations.AsEnumerable().
         Where(s => s.CountryId == Organization.CountryId && 
                    s.ProvinceId == Organization.ProvinceId && 
                    s.CityId == Organization.CityId && 
                    s.Name == Organization.Name)))

befor using where condition i used AsEnumerable() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文