Save Objects Relationship Only - Entity Framework

Asked

Viewed 464 times

2

Using the . Net platform with the Entity Framework.

I am persisting an A_B class object that relates to A and B as code below:

public class A_B
{
    private int Id;
    private string name;

    private A a;
    private B b;
}

public class A
{
    private int Id;
    private string name;
}

public class B
{
    private int Id;
    private string name;
}

The problem is that every time I include A_B, automatically, the Entity Framework includes B and A, but I don’t want to include B and A, because they already exist in the database. I am including only the relationship between these objects.

To save I use only these lines:

context.Entry(A_B).State = EntityState.Modified;

or

context.Entry(A_B).State = EntityState.Added;

DbContext.SaveChanges();
  • vote against Would specificationToSave be the object of the car as well? If yes, you would have to take the wheel codes that are in the car, and set in the specificationToSave object. Has how to pass more details like which parameters of the objects, maybe?

  • Follows the classes public class A_B { private int Id; private string name; private A a; private B b b; } public class A { private int Id; private string name; } public class B { private int Id; private string name; }

  • It is not duplicate. I will answer.

2 answers

1


Your modeling is wrong. See corrections below:

public class A_B
{
    [Key]
    public int Id { get; set; }; // Use propriedade, não campo.
    [Index("IUQ_AB_AId_BId", IsUnique = true, Order = 1)]
    public int AId { get; set; }; // Adicione.
    [Index("IUQ_AB_AId_BId", IsUnique = true, Order = 2)]
    public int BId { get; set; }; // Adicione.

    public string name { get; set; }; // Use propriedade, não campo.

    public virtual A a { get; set; }; // Use propriedade, não campo.
    public B b { get; set; }; // Use propriedade, não campo.
}

public class A
{
    public int Id { get; set; }; // Use propriedade, não campo.
    public string name { get; set; }; // Use propriedade, não campo.
}

public class B
{
    public int Id { get; set; }; // Use propriedade, não campo.
    public string name { get; set; }; // Use propriedade, não campo.
}

When mounting the object, two options:

1. Selecting associations and assigning as navigation properties

var a = context.As.FirstOrDefault(a => a.Id == idDeA);
var b = context.Bs.FirstOrDefault(b => b.Id == idDeB);
var a_b = new A_B { a, b };
context.As_Bs.Add(a_b);
context.SaveChanges();

2. Assign Ids on screen and save object

if (ModelState.IsValid)
{
    context.As_Bs.Add(a_b);
    context.SaveChanges();
}

Since there is no associative entity editing, it makes no sense to use context.Entry(A_B).State = EntityState.Modified;.

  • Gypsy, why not use field?

  • Because they can’t be marked as virtual. Suppose you use inheritance. Something like public class C : A { ... }, only that you need to add behaviors to some of the properties. As a field, this is impossible.

0

Brother, you will have to retrieve the object A or B from the database and leave it with track in context and add the object A_B to the recovered object, so Entity will understand that the recovered object already exists and knows that it should insert in its associative.

If you have a many table for many of the classes A and B, you can also retrieve one of the two objects from the database (leaving the track in context) and add the other to the recovered object, for example:

public void Teste()
    {
        var objA = _contexto.A.FisrtOrDefault(a => a.Id.Equals(a.Id));

        if(objA != null)
        {
            objA.ListaDeB.Add(objB);
            _contexto.SaveChanges();
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.