0

I'm not a "MacGyver", so I need help from how to do Frankenstein's tables work with circular reference works in EF. (facepalm) This is a old database on postgresql and I need it work on a new web sistem (!!!). So, I think I need a jerry-rigged here :)

Table A

int id PK;
varchar description;
varchar other_field
...
int idTableB FK (reference Table B key)

Table B

int id PK
varchar fieldX
double fiendY
...
int idTableA FK (reference Table A key)


If I try use models with ForeignKey attribute, get the error on attached image. enter image description here

Models with these error are like:

Models

[table("table_a")]
public class ModelA {

    [Key, Column("id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set;]

    [Column("idTableB")]
    public int IdTableB { get; set; }
    [ForeignKey("IdTableB")]
    public virtual ModelB Model_B { get; set; }

}

[table("table_b")]
public class ModelB {

    [Key, Column("id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set;]

    [Column("idTableA")]
    public int IdTableA { get; set; }
    [ForeignKey("IdTableA")]
    public virtual ModelA Model_A { get; set; }

}

Well, a object of ModelA refers to a object of ModelB with in turn makes references to another object of Model A, with in turn makes references to any ModelB object, may be the same as references it... OMG!

Yes! I'm working with DatabaseFirst. And it is a simple but very close to reality example. Has any magician here to help me to solve it? Thanks guys.

GustavoAdolfo
  • 361
  • 1
  • 9
  • 23
  • 1
    possible duplicate of [Unable to determine the principal end of an association - Entity Framework Model First](http://stackoverflow.com/questions/23505201/unable-to-determine-the-principal-end-of-an-association-entity-framework-model) – Vsevolod Goloviznin Jan 13 '15 at 16:10
  • Thank you @VsevolodGoloviznin, I had not found this topic in my searches. – GustavoAdolfo Jan 13 '15 at 17:19
  • Sorry, this technique not solved the problem :( "A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion." – GustavoAdolfo Jan 13 '15 at 17:42
  • http://stackoverflow.com/questions/27925286/using-2-different-versions-of-ef-in-the-same-project – Zakos Jan 13 '15 at 19:04
  • Thanks @Zakos but not is the topic... – GustavoAdolfo Jan 13 '15 at 19:09
  • oops , this one http://stackoverflow.com/questions/27088054/two-tables-referencing-each-other-in-entity-framework-6-code-first – Zakos Jan 13 '15 at 19:36
  • Possible duplicate of [Two tables referencing each other in Entity Framework 6 Code First](https://stackoverflow.com/questions/27088054/two-tables-referencing-each-other-in-entity-framework-6-code-first) – Andre Silva Apr 25 '18 at 17:59

2 Answers2

0

just set this on your dbcontext

db.Configuration.ProxyCreationEnabled = false;

0

In the constructor of context, verify this line:

Database.AutoTransactionsEnabled = false;

If the line is active, comment and try again.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94