简体   繁体   English

将域实体映射到多个表

[英]Mapping a domain entity to multiple tables

I've got a object (Filter) that's defined something like this 我有一个定义这样的对象(过滤器)

public class Filter
{
    public int FilterId { get; set; }

        public string Name { get; set; }

        public IList<User> Users { get; set; }
}

If possible, I'd like to store the object in multiple tables (sometimes spanning multiple rows) 如果可能的话,我想将对象存储在多个表中(有时跨多个行)

Given an initialization of the Filter like this: 给定过滤器的初始化是这样的:

Filter myFilter = new Filter();
myFilter.FilterId = 1;
myFilter.Name = "Name of my filter";
myFilter.Users.Add(new User("Joe", 10));
myFilter.Users.Add(new User("Jim", 20));

..I'd like it to be persisted in tables "Filter" and "Filter_User" ..我希望将其保留在表“ Filter”和“ Filter_User”中

Filter would end up with: 过滤器最终将具有:

1 Name of my Filter 1我的过滤器名称

Filter_User would contain Filter_User将包含

1 10 (ref. to Joe's ID) 1 20 (ref. to Jim's ID) 1 10(参考乔的ID)1 20(参考吉姆的ID)

I've been looking at the documentation and all I can find is using the element in my mapping file. 我一直在查看文档,而我所能找到的就是在映射文件中使用元素。 But as far as I can tell it will only let me do that with a component-relationship (ie Employee and EmployeeDetails - 1:1) 但据我所知,它只能让我使用组件关系(即Employee和EmployeeDetails-1:1)来做到这一点

Anyone to point me in the right direction? 有人给我指出正确的方向吗?

I think you're just looking for a many-to-many association ? 我认为您只是在寻找多对多关联? You're not constrained using a component relationship. 您不受组件关系的约束。

You could take a look at the many-to-many mapping type ? 您可以看一下多对多映射类型吗?

<set name="Users" table="Filter_User">
  <key column="filterid" />
  <many-to-many column="userid" class="User" />
</set>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM