如何使用 Fluent NHibernate 创建映射关系

发布于 2024-12-07 23:56:53 字数 859 浏览 1 评论 0原文

如何映射一对多关系 用户地址, 客户地址, 代理地址和 使用 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 技术交流群。

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

发布评论

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

评论(3

凉城已无爱 2024-12-14 23:56:53

我认为如果您希望能够对所有类型使用相同的地址,您需要独立存储关系来连接地址和用户/客户/机构。这涉及引入一个仅存储 ID 对的新表,并将其作为关系的存储位置。

您可以将集合映射为多对多,并使用 table 方法来命名链接表。您的映射将类似于:

public UserMap : ClassMap<User> {
  Id (u => u.Id);
  Map (u => u.Name);
  HasManyToMany (u => u.Addresses).Table ("UsersXAddresses");
}

您将需要一个类似的客户和代理链接表。

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:

public UserMap : ClassMap<User> {
  Id (u => u.Id);
  Map (u => u.Name);
  HasManyToMany (u => u.Addresses).Table ("UsersXAddresses");
}

You'll need a similar link table for customers and agencies.

很糊涂小朋友 2024-12-14 23:56:53

在映射类的构造函数中,使用 HasMany 方法映射链接。 HasMany 将创建一对多关系。 HasManyToMany 将创建多对多关系。

例如: HasMany(x => x.Address).LazyLoad();

这将在 User 之间创建一对多关系code> 类和 Address 类。

对于多对多,您还需要指定表名称,如果需要,还需要指定左侧表映射和右侧表映射。

例如: HasManyToMany(x => x.Address).Table("AddressToUser").ParentKeyColumn("AddressId").ChildKeyColumn("UserId").LazyLoad();< /code>

如果您决定要在 UserAddressAgencyAddress 之间设置区别(其中这些是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 the Address 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 an AgencyAddress (where these are sub-classes of Address) - you can use the DiscriminateSubClassesOnColumn method in the AddressMap 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();

↘人皮目录ツ 2024-12-14 23:56:53

您应该能够使用 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.

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