Entity Framework 4覆盖自己类属性的Equals和GetHashCode
我正在使用带有 .NET 4 和 Entity Framework 4 的 Visual Studio 2010。我正在使用 POCO 类,而不是 EF4 生成器。我需要重写 Equals()
和 GetHashCode()
方法,但这实际上不起作用。我以为这是每个人都会做的事情,但我在网上没有找到任何有关该问题的信息。
当我编写自己的类和 Equals
方法时,我使用属性的 Equals()
,该属性需要由 EF 加载才能填充。就像这样:
public class Item
{
public virtual int Id { get; set; }
public virtual String Name { get; set; }
public virtual List<UserItem> UserItems { get; set; }
public virtual ItemType ItemType { get; set; }
public override bool Equals(object obj)
{
Item item = obj as Item;
if (obj == null)
{
return false;
}
return item.Name.Equals(this.Name)
&& item.ItemType.Equals(this.ItemType);
}
public override int GetHashCode()
{
return this.Name.GetHashCode() ^ this.ItemType.GetHashCode();
}
}
该代码不起作用。问题出在 Equals
和 GetHashCode
中,我尝试从 ItemType
获取 HashCode
或 Equal
代码>.每次,如果我尝试通过 Linq2Entites 获取数据,我都会收到 NullRefernceException。
修复此问题的一种肮脏方法是捕获 NullReferenceException 并返回 false
(通过 Equals)并返回 base.GetHashCode()
(通过 GethashCode
) ,但我希望有更好的方法来解决这个问题。
我编写了一个小测试项目,其中包含用于 DB 和 POCO 域的 SQL 脚本、EDMX 文件和控制台测试主要方法。您可以在这里下载: 下载
I’m using Visual Studio 2010 with .NET 4 and Entity Framework 4. I’m working with POCO Classes and not the EF4 Generator. I need to override the Equals()
and GetHashCode()
methods, but that doesn’t really work. I thought it’s something everybody does, but I don’t find anything about the problem online.
When I write my own classes and Equals
method, I use Equals()
of property’s, which need to be loaded by EF to be filled. Like this:
public class Item
{
public virtual int Id { get; set; }
public virtual String Name { get; set; }
public virtual List<UserItem> UserItems { get; set; }
public virtual ItemType ItemType { get; set; }
public override bool Equals(object obj)
{
Item item = obj as Item;
if (obj == null)
{
return false;
}
return item.Name.Equals(this.Name)
&& item.ItemType.Equals(this.ItemType);
}
public override int GetHashCode()
{
return this.Name.GetHashCode() ^ this.ItemType.GetHashCode();
}
}
That Code doesn’t work. The problems are in Equals
and GetHashCode
where I try to get HashCode
or Equal
from ItemType
. Every time, I get a NullRefernceException if I try to get data by Linq2Entites.
A dirty way to fix it is to capture the NullReferenceException and return false
(by Equals) and return base.GetHashCode()
(by GethashCode
), but I hope there is a better way to fix this problem.
I’ve written a little test project, with SQL Script for the DB and POCO Domain, EDMX File and Console Test Main Method. You can download it here:
Download
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是纯 POCO 类,没有代理生成。在这种情况下,不支持延迟加载,您必须自己创建存储库方法来加载相关实体。这就是为什么您的
ItemType
实体为 null(并且始终为 null)。如果您想要延迟加载,可以使用 EF 生成器构建支持延迟加载的 POCO 类。
You're using pure POCO classes without proxy generation. In this case lazy loading is not supported, and you will have to create repository methods to load related entities yourself. That's why your
ItemType
entity is null (and always will be).If you want lazy loading, you can use the EF generator to build POCO classes that support lazy loading.