0

I am a beginner in Entity Framework Code First. So I am having troubles in working with migrations. So this is an issue:
I have following models:

Defect - table of defects(general)
User - table of users
Order - table or orders
DefectEntry - defects per order(entered by a user).

DefectEntry has:

public virtual Defect defect { get; set; }
public virtual User user { get; set; }
public virtual Order order { get; set; }

All the other models contain:

public virtual ICollection<DefectEntry> Entries { get; set; }

When I am trying to add a defect per order(order & user known) the EF is creating a new order and user objects with compeletely another IDs(Because ID is unique per user and treated as Primary_key).

 private Order order;
 private User user;

public AddDefect(ref Order order, ref User user) //this is a name of UserControl
    {
        InitializeComponent();
        this.order = order;
        this.user = user;
        loadDefects();
    }

private void loadDefects()
    {
        using (MyBridgeContext context = new MyBridgeContext())
        {
            var defects = context.Defects;
            foreach (var obj in defects)
            {
                defectList.Items.Add(obj);
            }
        }
    }

private void addButton_Click(object sender, RoutedEventArgs e)
    {
        if (doubleCheck.IsChecked == true)
        {
            try
            {
                using (MyBridgeContext context = new MyBridgeContext())
                {
                    DefectEntry entry = new DefectEntry();

                    foreach (Defect def in defectList.Items)
                    {
                        if(defectList.SelectedItem == def)
                        {
                            entry.defect = def;
                        }
                    }

                    entry.user = user;
                    entry.order = order;
                    entry.dt = DateTime.Now;

                    context.Entries.Add(entry);
                    context.SaveChanges();


                    this.Content = new MainMonitoring(ref order, ref user);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

When I push an addButton I have these kind of duplicates: User table look Defects table look Order table look Defect Entry table look

1

1 Answer 1

0

It does this because your user and order entities are disconnected from EF context. You have to reattach them to the context before using them in DefectEntry object. So you have to do something like this right after using (MyBridgeContext context=new MyBridgeContext()) { statement inside the addButton_Click method:

context.Users.Attach(user);
context.Orders.Attach(order);
3
  • thanks so much it worked fine but I get another error, now: Sep 27, 2018 at 5:59
  • Context.Defects.Attach(defect) is not working. here: Sep 27, 2018 at 6:01
  • DefectEntry entry = new DefectEntry(); foreach (Defect def in defectList.Items) { if (defectList.SelectedItem == def) { context.Defects.Attach(def); entry.defect = def; } } Sep 27, 2018 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.