在模型中,我有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表。 我知道我應該先在Order and Position表中寫入數據。 然後,從這些數組中獲得兩個鍵,我應該讀取它們並將它們作爲外鍵保存,並將它們發送到OrderPosition。 這些說明的記錄應該是什麼樣的?
想必它看起來像是一系列的INSERT語句。這似乎與MVC或ASP.NET無關,特別是它低於堆棧。我們真的在談論實體框架嗎?如果是這樣,請適當標記問題。 (是的,這是一個MVC應用程序,但它不是MVC給你的問題)。無論如何,在這種情況下,會發生一系列「添加」,然後是「SaveChanges」語句。您可以將OrderPosition對象添加到列表中的Order或Position對象,並保存應實現它的主對象。查看許多許多EF的例子,有很多已經在線。 – ADyson
有3個我沒有找到的示例表。所以請幫助。 –
谷歌的第一個結果:https://stackoverflow.com/questions/4253165/insert-update-many-to-many-entity-framework-how-do-i-do-it。通常情況下,EF不會像您所得到的那樣生成第三個實體OrderPosition。你有沒有先使用代碼(你寫的類和EF創建數據庫)或數據庫優先(你寫的數據庫和EF編寫的類)? – ADyson