Click here to Skip to main content
15,884,955 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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; }
           //many to many
            public virtual ICollection<Position> PositionList { 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> OrderList { get; set; }
        }
        //Map many to many
        base.OnModelCreating(modelBuilder);
                
                    modelBuilder.Entity<Order>()
                       .HasMany<Position>(s => s.PositionList)
                       .WithMany(c => c.OrderList)
                       .Map(cs =>
                       {
                           cs.ToTable("OrderPosition");
                           cs.MapLeftKey("IdOrder");
                           cs.MapRightKey("IdPosition");
                       });


What I have tried:

[HttpPost]
               [ValidateAntiForgeryToken]
               public PartialViewResult _AddPost(int IdOrder)
               {
                   var findOrder = db.Order.Find(IdOrder);
                   if (ModelState.IsValid)
                   {
                       Position position = new Position { Description = "Test"};
                       db.Position.Attach(position);
                       findOrder.PositionList.Add(position);
                       db.SaveChanges();

                   }
                   return PartialView();
               }


I tried as above but it does not work
Posted
Updated 27-Sep-17 1:31am
v3

1 solution

db.Position.Attach(position);


change on

<pre>db.Position.Add(position);


Great work.
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 27-Sep-17 7:39am    
In entityframework the table is taken like Positions
db.Positions.Add(position);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900