Write only object relationships - Entity Framework [duplicate]

2

Using the .Net platform with the Entity Framework.

I'm persisting an object of class A_B 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 add A_B, the Entity Framework automatically includes B and A, but I do not 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 only use these lines:

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

or

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

DbContext.SaveChanges();
    
asked by anonymous 15.05.2017 / 19:52

2 answers

1

Your modeling is wrong. See the 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. Select associative entities and assign them 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 to screen and save object

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

As there is no associative entity editing, it does not make sense to use context.Entry(A_B).State = EntityState.Modified; .

    
16.05.2017 / 23:08
0

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

If you have a many to many table of classes A and B, you can also retrieve one of two objects from the database (leaving the track in context) and adding the other one to the retrieved 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();
        }
    }
    
16.05.2017 / 22:49