C# MVC2 如何在不同列中添加两个相关表的数据?
我想将数据插入两个相关的表中。我的第一个表是资产;该表有 14 列,例如。 assetid
、assettype
、assetsize
等。第二个表是 Owner
表,列为 OwnerId
、assetid
、ownername
、contactno< /code>、
地址
和 emailid
。
我的 assetid 列(来自 asset 表)是第一个表的主键。 Ownerid
也是第二个表上的主键;这些是自动生成的。
我希望第二个表获取第一个表和我填写的另一列中的资产 ID。我的所有列都被声明为not null
。请帮助我如何在表中存储数据。
I want insert data into two, related tables. My first table is asset
; this table has 14 columns, eg. assetid
, assetype
, assetsize
, and so on. The second table is the Owner
table, the columns are OwnerId
, assetid
, ownername
, contactno
, address
and emailid
.
My asseetid
column (from the asset
table) is the primary key of the first table. Ownerid
is also a primary key, on the second table; these are auto generated.
I want the second table to get the assetid
in first table and another column I filled. All my columns are declared not null
. Please help me how to store data in my tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要告诉我们资产对所有者的基数;是一对一、一对多还是多对多?
我认为这听起来像多对多——每个资产可以有多个所有者,每个所有者可以拥有多个资产。如果是这样的话,你需要创建一个连接表;根据您的 ORM(实体、NHibernate 等),有不同的方法可以实现。
我只使用过 NHibernate 和 ActiveRecord,所以我只能对此发表评论。您需要创建一个具有两个外键引用(assetid 和 Ownerid)的联接表(asset_owner),并且需要使用 HasMany 和 BelongsTo 注释您的类,如下所示: http://www.castleproject.org/activerecord/gettingstarted/relations.html
You need to tell us the cardinality of asset to owner; is it one-to-one, one-to-many, or many-to-many?
I think it sounds like many-to-many -- each asset can have many owners, and each owner can have many assets. If that's the case, you need to create a join table; depending on your ORM (Entity, NHibernate, etc.) there are different ways of doing it.
I've only used NHibernate with ActiveRecord, so I can only comment on that. You need to create a join table (asset_owner) with two foreign key references (assetid and ownerid), and you need to annotate your classes with HasMany and BelongsTo, like so: http://www.castleproject.org/activerecord/gettingstarted/relations.html