是否可以使用 EF4.x 映射值对象的集合?

发布于 2025-01-06 07:50:37 字数 190 浏览 3 评论 0原文

我找不到映射值对象集合的方法,可能吗?

public class AnEntity
{
    public int Id {get;set;}
    public ICollection<Guid> Values {get;set;} // <-- this
}

谢谢, E.

I can't find a way to map a value object collection, is it possible?

public class AnEntity
{
    public int Id {get;set;}
    public ICollection<Guid> Values {get;set;} // <-- this
}

Thanks,
E.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

往事风中埋 2025-01-13 07:50:37

因为这是不可能的。您只能映射实体的集合(带有键的类)。您可以通过使用特殊实体并公开第二个属性来解决此问题,该属性将为您提供投影:

public class SecondEntity {
    public Guid Id { get; set; }
}

public class AnEntity {
    public int Id { get; set; }
    public virtual ICollection<SecondEntity> Values { get; set; }

    public IEnumerable<Guid> GuidValues { 
        return Values.Select(v => v.Id);
    }
}

如果您预计集合会很小,您也可以使用单个字符串字段而不是相关集合并使用 String.Split , String.Join 提供集合模拟。

Because it is not possible. You can map only collection of entities (classes with key). You can solve this by using special entity and exposing second property which will provide projection for you:

public class SecondEntity {
    public Guid Id { get; set; }
}

public class AnEntity {
    public int Id { get; set; }
    public virtual ICollection<SecondEntity> Values { get; set; }

    public IEnumerable<Guid> GuidValues { 
        return Values.Select(v => v.Id);
    }
}

If you expect that collection will be small you can also use single string field instead of related collection and use String.Split, String.Join to provide emulation of collection.

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