简体   繁体   中英

asp.net MVC3 application flow

I am working on an asp.net application. I need help designing the database related model and linq queries.

I have three tables ( and two lookup tables).

1)Product header (product header id as Primary key) 2) Product Detail(it has product header id as foreign key) 3) product attachment (it has product detail id as foreign key)

Now, I need to insert record in db.

a) for one Product header record there can be multiple Product detail records b) for multiple product detail records, there can be multiple attachments.

I have created three entities for each tables. Product header also has two keys from user table and history table. but on view I need to show the user name instead of the key. Should I create a view model class which will hold all these entity classes as properties and how can I make sure that there first record is inserted in product header, then product details and then product attachment ?

Please suggest.

Thanks

an easy way to do this is to use the new Code First Feature that comes with EF 4.1

you can create your "entities" like so:

public class ProductHeader
{
   public int ID {get; set;}

   public virtual ICollection<ProductDetail> ProductDetails {get; set;}

   //Other properties
}

public class ProductDetail
{
   public int ID {get; set;}

   public virtual ICollection<ProductAttachment> ProductAttachments {get; set;}

   //Other properties
}

public class ProductAttachment
{
   public int ID {get; set;}

   // you can have a navigation property here for the user, this allows you to access his name
   public virtual User User {get; set;}
   //Other properties
}

public class MyContext:DbContext
{
    public DbSet<ProductHeader> ProductHeaders {get; set;}
    public DbSet<ProductDetail> ProductDetails {get; set;}
    public DbSet<ProductAttachment> ProductAttachment {get; set;}
}

for the insertion order, just add your ProductHeader (after adding the ProductDetails and ProductAttachments to it) and EF will take care of it.

EDIT : here's a sample code for adding a ProductHeader:

var context=new MyContext();
var ph=new ProductHeader();
ph.User=user;
var pd=new ProductDetail();
pd.ProductAttachments.Add(new ProductAttachment());
ph.ProductDetails.Add(pd);
context.ProductHeaders.Add(ph);
context.SaveChanges();

Hope this helps.

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