开发者

Comparing foreign keys in entity framework 3.5

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.EntityKeyV开发者_如何学Calues[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?


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜