简体   繁体   中英

C# MVC2 how to add data two related table in different column?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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