3

I have such entity:

 public class Entity1
    {
        public short Id { get; set; }
        public int Entity2Id { get; set; }
        public virtual Entity2 Entity2 { get; set; }
    }

And have such one to Many relationship:

  this.HasRequired(m => m.Entity2)
            .WithMany()
            .HasForeignKey(m => m.Entity2Id)
            .WillCascadeOnDelete(false);

And here is the thinkg which I cannot understand:

For example, evertyhting works fine if I have changed the entity state to Added firstly:

context.Entry(entity1).State = System.Data.EntityState.Added;
entity1.Entity2.Id = 541;

 // Since this line `entity1.Entity2Id` value is 0.
 context.Entry(entity1.Entity2).State = System.Data.EntityState.Unchanged;
 // But now everything is fine, because `entity1.Entity2Id` value is 541 also.

 context.Entry(entity1).State = System.Data.EntityState.Unchanged;

But, I don't want to change state to Added, when I am removing the first line, this exception occured:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

Beacuse, entit1.Entity2Id != entity1.Entity2.Id.

And, I don't want manually to change the value.

How, I can make it work without changing the state to Added?

Update:

I have investigated this problem more. From this So question:

This is called property fixup, and used to be done automatically by the generated proxies. However, with DbContext this is no longer the case. According to this Connect issue, this is by design.

Hello, The DbContext template actually doesn't generate classes that will be used as change tracking proxies - just lazy loading proxies (which don't do fix-up). We made this decision because change tracking proxies are complex and have a lot of nuances that can be very confusing to developers. If you want fix-up to occur before SaveChanges you can call myContext.ChangeTracker.DetectChanges. ~EF Team

An alternative is to call DbContext.Entry(entity), which will sync up the entity. This is described in this article: Relationships and Navigation Properties under "Synchronizing the changes between the FKs and Navigation properties"

Community
  • 1
  • 1
  • What version of EF are you using? – wahwahwah Jun 14 '15 at 05:16
  • @wahwahwah I am using EF 5.0 – Yay Konedensinoe Jun 14 '15 at 05:17
  • did you take a look at [this post](http://stackoverflow.com/questions/13258314/both-one-to-one-and-one-to-many-relationships-in-entity-framework-5-code-first)? depending on your EF config you might need to drop your current context and rerun it – wahwahwah Jun 14 '15 at 05:24
  • @wahwahwah I don't understand you. First of all I didn't find any sentence about dropping context in your link. And the second one is that IMHO I will lost all entities if I will drop entity. – Yay Konedensinoe Jun 14 '15 at 05:28
  • both entities are originally detached? – jjj Jun 14 '15 at 05:46
  • @jjj Yes, they have not any relation with context yet the first line. – Yay Konedensinoe Jun 14 '15 at 05:48
  • Can you set `entity1.entity2Id = 541` then attach `entity1` onto the context? – jamesSampica Jun 14 '15 at 06:34
  • @Shoe No, then it is giving the above exception in my answer, because, it will not change `entity1.Entity2.Id`. – Yay Konedensinoe Jun 14 '15 at 06:44
  • Are entity1 & entity2 new or existing in the db? – jamesSampica Jun 14 '15 at 07:08
  • @Shoe The procedure is. `Entity1` is coming to my method from web service for inserting or updating in the database. But, I searched the database and found that Entity2 is already existing in the database. So, I set it's ID and also it's state to unchanged. My method works fine with reflection. The one problem is that, I don't want to set entity's state to added. To tell the truth it is not a problem also, because y method correctly changes it's state to modified or unchanged. But, I can't understand how **does EF detects changes and why it is detecting after adding not attaching?** – Yay Konedensinoe Jun 14 '15 at 07:15

0 Answers0