开发者

asp.net MVC3 application flow

开发者 https://www.devze.com 2023-02-25 10:10 出处:网络
I am working on an asp.net application. I need help designing the database related model and linq queries.

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 fi开发者_如何学运维rst 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号