2017-09-05 17 views
1

モデルでは、私は多くのテーブルとの関係で3つのテーブルを持っています。MVCの多対多関係 - 外部キーを持つテーブルへの書き込み

public class Order 
{ 
    [Key] 
    public int IdOrder { get; set; } 
    public string UserId { get; set; } 
    public virtual User User { get; set; } 
    public int IdOrderAttachment { get; set; } 
    public virtual OrderAttachment OrderAttachment { get; set; } 
    public virtual ICollection<Employee> Employee { get; set; } 

    [Required(ErrorMessage = "Specify the date of order acceptance")] 
    [Display(Name = "Date of acceptance of the order")] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTimeDateOfAcceptance { get; set; } 

    [Display(Name = "Date of completion planning")] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime? DateOfCompletionPlanning { get; set; } 

    [Display(Name = "End Date")] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
    public DateTime? EndDate { get; set; } 

    [Required(ErrorMessage = "Enter the subject")] 
    [MaxLength(200, ErrorMessage = "Name max 200 characters")] 
    [Display(Name = "Subject")] 
    public string Subject { get; set; } 

    public virtual ICollection<OrderPosition> OrderPosition{ get; set; } 
} 

public class OrderPosition 
{ 
    [Key] 
    public int IdOrderPosition { get; set; } 
    public int IdOrder { get; set; } 
    public int IdPosition { get; set; } 
    public virtual Order Order { get; set; } 
    public virtual Position Position { get; set; } 
} 

public class Position 
{ 
    [Key] 
    public int IdPosition { get; set; } 
    [Column(TypeName = "nvarchar(MAX)")] 
    [Display(Name = "Description")] 
    [UIHint("tinymce_jquery_full"), AllowHtml] 
    public string Description { get; set; } 
    public virtual ICollection<OrderPosition> OrderPosition { get; set; } 
} 


[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult CreateOrder(HttpPostedFileBase file, DataOrderUserViewModel viewModel) 
{ 
    var userId = User.Identity.GetUserId();   

    if (ModelState.IsValid) 
    { 
     if (file != null && file.ContentLength > 0) 
     { 
      string path = "/Content/Layout/CompanyFile"; 
      if (!Directory.Exists(HttpContext.Server.MapPath(path))) 
      { 
       Directory.CreateDirectory(HttpContext.Server.MapPath(path)); 
      } 
      string filename = Path.GetFileName(file.FileName); 

      file.SaveAs(Path.Combine(HttpContext.Server.MapPath(path), filename)); 
      viewModel.NameFile = path+ "/" + filename; 

      //var nameFile = Path.GetFileName(file.FileName); 

      //var path = Path.Combine(Server.MapPath("/Content/Layout/CompanyFile"), nameFile); 

      //file.SaveAs(path); 

     } 

     var order = new Order() 
     { 
      DateTimeDateOfAcceptance = viewModel.DateTimeDateOfAcceptance, 
      Subject= viewModel.Subject,     
      UserId = userId 

     }; 

     var position = new Position() 
     { 
      Description = viewModel.Description 
     }; 

     var orderAttachment = new OrderAttachment() 
     { 
      NameFile= viewModel.NameFile, 
      Description = viewModel.Description2 
     }; 

     db.Order.Add(order); 
     db.Position.Add(position); 
     db.OrderAttachment.Add(orderAttachment); 
     db.SaveChanges(); 

    } 
    return RedirectToAction("Index", "Administration"); 
} 

私の質問は、データをOrderPositionテーブルに書き込む方法です。 注文とポジションテーブルにデータを書き込む必要があることを理解しています。 次に、これらの配列から2つのキーを読んで、OrderPositionにそれらを保存する外部キーとして送信します。 これらの命令の記録はどのように見えるでしょうか?

+0

おそらく、一連のINSERT文のように見えます。これは具体的にはMVCやASP.NETとは無関係のようですが、スタックが低くなっています。エンティティフレームワークについて実際に話していますか?そうであれば、問題に適切なタグを付けてください。 (はい、それはMVCアプリケーションですが、MVCでは問題ありません)。とにかく、その場合は一連の 'Add'と' SaveChanges'ステートメントです。リスト内のOrderオブジェクトまたはPositionオブジェクトにOrderPositionオブジェクトを追加し、それを達成する必要があるメインオブジェクトを保存することができます。多くの多くのEFの例を見て、すでに多くのオンラインがあります。 – ADyson

+0

私は見つけられていない3つのサンプルテーブルを使用しています。だから助けてください。 –

+0

googleの最初の結果:https://stackoverflow.com/questions/4253165/insert-update-many-to-many-entity-framework-how-do-i-do-it。通常、EFはあなたが持っているように3rd Entity OrderPositionを生成しません。あなたはコードを使用しましたか(クラスを作成し、EFがDBを作成しました)、データベースを最初に使用しましたか(データベースを書き、EFがクラスを書きましたか)? – ADyson

答えて

0

私の場合、以下の関係を定義する必要がありますか?

modelBuilder.Entity<OrderPosition>() 
     .HasKey(c => new { c.IdOrder , c.IdPosition }); 

    modelBuilder.Entity<Order>() 
     .HasMany(c => c.Subject) 
     .WithRequired() 
     .HasForeignKey(c => c.IdOrder); 

    modelBuilder.Entity<Position>() 
     .HasMany(c => c.Description) 
     .WithRequired() 
     .HasForeignKey(c => c.IdPosition); 
+0

これをあなたの質問に移動してください。それは答えではありません。 –

+0

新しい質問がある場合は、[質問する](https://stackoverflow.com/questions/ask)ボタンをクリックして質問してください。コンテキストを提供する場合は、この質問へのリンクを含めてください。 - [レビューの投稿](/レビュー/低品質の投稿/ 17241666) – Liam

関連する問題