如何使用 Fluent NHibernate 创建映射关系
如何映射一对多关系 用户地址, 客户地址, 代理地址和 使用 Fluent NHibernate 存储在单个地址表中
public class User
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Address> Address { get; set; }
}
public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Address> Address { get; set; }
}
public class Agency
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Address> Address { get; set; }
}
public class Address
{
public virtual int Id { get; set; }
public virtual string Address1 { get; set; }
public virtual string Address2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
}
How can I map one-to-many relationship with
User to Address,
Customer to Address,
Agency to Address and
store in a single Address Table using Fluent NHibernate
public class User
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Address> Address { get; set; }
}
public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Address> Address { get; set; }
}
public class Agency
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Address> Address { get; set; }
}
public class Address
{
public virtual int Id { get; set; }
public virtual string Address1 { get; set; }
public virtual string Address2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为如果您希望能够对所有类型使用相同的地址,您需要独立存储关系来连接地址和用户/客户/机构。这涉及引入一个仅存储 ID 对的新表,并将其作为关系的存储位置。
您可以将集合映射为多对多,并使用 table 方法来命名链接表。您的映射将类似于:
您将需要一个类似的客户和代理链接表。
I think you'll need to store your relations independently to connect addresses and users/customers/agencies if you want to be able to use the same address for all types. This involves introducing a new table that only stores ID pairs, and making that the storage location for your relationship.
You can map collections as many-to-many and use the table method to name your link table. Your mapping would look something like:
You'll need a similar link table for customers and agencies.
在映射类的构造函数中,使用
HasMany
方法映射链接。HasMany
将创建一对多关系。HasManyToMany
将创建多对多关系。例如:
HasMany(x => x.Address).LazyLoad();
这将在
User
之间创建一对多关系code> 类和Address
类。对于多对多,您还需要指定表名称,如果需要,还需要指定左侧表映射和右侧表映射。
例如:
HasManyToMany(x => x.Address).Table("AddressToUser").ParentKeyColumn("AddressId").ChildKeyColumn("UserId").LazyLoad();< /code>
如果您决定要在
UserAddress
和AgencyAddress
之间设置区别(其中这些是Address) - 您可以使用AddressMap
类中的DiscriminateSubClassesOnColumn
方法,以便 FNH 知道要创建一个额外的列,以确定要创建哪种类型的对象。例如:
DiscriminateSubClassesOnColumn("Type").AlwaysSelectWithValue();
In the constructor for your mapping class, map the link using the
HasMany
method.HasMany
will create a one-to-many relationship.HasManyToMany
will create a many-to-many relationship.For example:
HasMany(x => x.Address).LazyLoad();
This will create a one-to-many relationship between the
User
class and theAddress
class.For the many-to-many, you will also need to specify the table name and if you so desire, the left and right side table mappings.
For example:
HasManyToMany(x => x.Address).Table("AddressToUser").ParentKeyColumn("AddressId").ChildKeyColumn("UserId").LazyLoad();
If you decide that you want to set up a distinction between a
UserAddress
and anAgencyAddress
(where these are sub-classes of Address) - you can use theDiscriminateSubClassesOnColumn
method in theAddressMap
class so the FNH knows to create an extra column in order to determine which type of object to create.For example:
DiscriminateSubClassesOnColumn("Type").AlwaysSelectWithValue();
您应该能够使用 FNH Automapping “按原样”映射这些类。
它将处理对象模型中的所有关系。
我相信它会根据您的需要将所有地址放入一个地址表中,但不能肯定。
You should be able to use FNH Automapping to map these classes "as is".
It will handle all the relationships in your object model.
I believe it will put all the addresses in a single Address table as you desire, but can't say for sure.