2011-10-07 26 views
0

我想改变.Where方法对于特定情况的默认行为。如何在EntityFramework 4.1中更改.Where方法的默认行为?

我所有的业务对象从BaseObject继承了财产int ID {get; set;}

// Base class 
public abstract class BaseObject 
{ 
    public abstract int ID {get; set;} 
} 

我有2类:

public partial class User : BaseObject 
{ 
    public override int ID {get; set;} 
    public string Username { get; set; } 
    public int ProfileID { get; set; } 

    public virtual Profile Profile { get; set; } 
} 

public partial class Profile : BaseObject 
{ 
    public override int ID { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<User> Users { get; set; } 

    public static Profile GetAdminProfile() 
    { 
     return new Profile(){ID = 3, Name = "Admin profile"}; 
    } 
} 

我还想写

// This throws Unable to create a constant value of type 'Profile'... exception 
User admin = Users.Where(user.Profile == Profile.GetAdminProfile()).FirstOrDefault(); 

,而不是

User admin = Users.Where(user.Profile.ID == Profile.GetAdminProfile().ID).FirstOrDefault(); 

有没有办法做到这一点?

回答

0

这是Entity Framework中的一个已知问题。你将会遵循第二种方法。

+0

是的,似乎是这样。无论如何,感谢您的支持。 –