1

I have a code-first EF model like this:

public partial class A
{
    [Key]
    [StringLength(7)]
    public string Code { get; set; }

    [Required]
    [StringLength(100)]
    public string Name { get; set; }


    public virtual ICollection<B> Bs { get; set; }
}

public partial class B
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    [Required]
    [StringLength(7)]
    [ForeignKey("A")]
    public string ACode { get; set; }

    public virtual A A { get; set; }
}

When I am inserting data like this:

var a = new A();
a.Code = "A0";
a.Name = "A Name";

var b = new B();
b.Name = "B Name";
b.ACode = a.Code;
b.A = a;

using (DbContext context = new DbContext())
{
        context.As.Add(a);
        context.Bs.Add(b);
        context.SaveChanges();
}

var b2 = new B();
b2.Name = "B Name 2";
b2.ACode = a.Code;
b2.A = a;

using (DbContext context = new DbContext())
{
        context.Bs.Add(b2);
        context.SaveChanges();
}

I have no problem during first context.SaveChanges. But when I try to add b2 with same ACode and reference I get

Violation of PRIMARY KEY constraint 'PK_dbo.As'. Cannot insert duplicate key in object 'dbo.As'. The duplicate key value is (A0 ).

I'm really confused about that do so I would be very glad if someone could explain why on the second call to context.SaveChanges(), EF is trying to insert new A entity instead of using the already existing one.

Thank you very much for your help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Teamol
  • 733
  • 1
  • 14
  • 42
  • after the first SaveChanges, A has an Id value. because you are implicitly adding the same object again in the second call, you are getting the exception. you should keep the context open or set the state of A to modified. – DevilSuichiro Oct 17 '15 at 11:02
  • A has no Id and key should be the Code. I cannot have same context whenever I want to add entity B to the database. I want to be able to add entity B with reference to A any time not only when I'm also inserting entity A to the database. – Teamol Oct 17 '15 at 11:15
  • the second context has no understanding about `a`, it assumes `a` should be added and hence the exception. – King King Oct 17 '15 at 11:17
  • Ok and how do I let the second context know that `a` is a reference. If I would replace `string Code` with `int Id` it would work automatically. – Teamol Oct 17 '15 at 11:19
  • 2
    try `context.As.Attach(a)` before `context.Bs.Add(b2);` to see if it works. – King King Oct 17 '15 at 11:22

1 Answers1

2

So thanks to King King comment I found out two things.

First is that I can automatically find foreign key if only Foreign Id is assigned (not reference).

var b2 = new B();
b2.Name = "B Name 2";
b2.ACode = a.Code;

instead of

var b2 = new B();
b2.Name = "B Name 2";
b2.ACode = a.Code;
b2.A = a;

Second is that when you assign reference you have to Attach references object using context.As.Attach(a) before context.Bs.Add(b2);

Thank you very much king king your post helped me alot.

Teamol
  • 733
  • 1
  • 14
  • 42