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();
有沒有辦法做到這一點?
是的,似乎是這樣。無論如何,感謝您的支持。 –