将数组映射到单行

发布于 2024-08-15 02:20:38 字数 666 浏览 3 评论 0原文

我有以下课程:

public class InventoryItem 
{ 
     private Usage[] usages = new Usage[12]; 
     virtual public Usage[] Usages { get { return usages; }} 
     virtual public string Name{get;set;} 
} 

public class Usage 
{ 
     virtual public double Quantity{get;set;} 
     virtual public string SomethingElse{get;set;} 
} 

我知道Usages.Length将始终是12。我认为这是最好的 像这样将其存储在数据库中:

Name nvarchar(64), 
Usage_Quantity_0 float, 
Usage_SomethingElse_0 nvarchar(16), 
Usage_Quantity_1 float, 
Usage_SomethingElse_1 nvarchar(16), 
... 
Usage_Quantity_11 float, 
Usage_SomethingElse_11 nvarchar(16), 

我怎样才能完成这个工作?

I have the following classes:

public class InventoryItem 
{ 
     private Usage[] usages = new Usage[12]; 
     virtual public Usage[] Usages { get { return usages; }} 
     virtual public string Name{get;set;} 
} 

public class Usage 
{ 
     virtual public double Quantity{get;set;} 
     virtual public string SomethingElse{get;set;} 
} 

I know that Usages.Length will always be 12. I think it would be best
to store it in the DB like so:

Name nvarchar(64), 
Usage_Quantity_0 float, 
Usage_SomethingElse_0 nvarchar(16), 
Usage_Quantity_1 float, 
Usage_SomethingElse_1 nvarchar(16), 
... 
Usage_Quantity_11 float, 
Usage_SomethingElse_11 nvarchar(16), 

How can I get this done?

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

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

发布评论

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

评论(1

深爱不及久伴 2024-08-22 02:20:38

一个在数组和 12 个私有字段之间进行转换的 getter/setter 怎么样?在Hibernate中(我不知道NHibernate),映射可以放在字段上,你可以使用 可嵌入实体,将Usage映射到InventoryItem中的2列。如果可嵌入实体在 NHibernate 中不可用,那么您将需要 24 个字段。

即使存在固定基数,一对多关系可能仍然更可取。如果担心性能,可以搜索一下N+1问题和eager fetching。

What about a getter/setter that converts to/from an array and 12 private fields? In Hibernate (I don't know about NHibernate), the mapping can be placed on the field and you can use embeddable entities to have Usage be mapped to 2 columns in InventoryItem. If embeddable entities are not available in NHibernate, then you will need 24 fields.

A 1-to-many relationship is maybe still preferable even if there is a fixed cardinality. If you are worried about performance, search for the N+1 problem and eager fetching.

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