Home > Software engineering >  EF Core 1:1 Relation?
EF Core 1:1 Relation?

Time:10-05

The tables i want are looking like this...

Identity  | Id (PK), Tag
Character | IdentityId (FK, PK), Health

The character table should reference exactly one single row of the identity table... and the identity table should not reference anything else 1:0.

My current model is looking like this...

    /// <summary>
    /// Represents an identity in our database. 
    /// </summary>
    public class Identity {

        public long Id { get; set; }
        public string Tag { get; set; }
    }
    
    /// <summary>
    /// Represents an character ingame with all his attributes. 
    /// </summary>
    public class Character {
        
        public Identity Identity { get; set; }

        public float Health { get; set; }
    }

    modelBuilder.Entity<Identity>(entity => {

          entity.ToTable("identity");
          entity.HasKey(e => e.Id);
    });
            
     modelBuilder.Entity<Character>(entity => {

          entity.ToTable("character");
          // entity.HasKey(e -> e.Identity.Id); DOES NOT WORK
          entity.Navigation(character => character.Identity).AutoInclude();
     });            

The problem with this is that the reference to the identity inside the character does not count as an primary key... neither a foreign key.

e -> e.Identity.Id does not work for some reason and results in an error telling me that this is not possible.

I want that the Identity inside the Character counts as his primary key, while still being a reference to an row inside the Identity-Table ( Foreign key ). The identity table however should not reference the character.

Is this possible ? If so... how ?

CodePudding user response:

You can create a property in your Identity class

 public class Identity {
        public long Id { get; set; }
        public string Tag { get; set; }

        public Character Character { get; set; }
    }

then your model become

modelBuilder.Entity<Identity>()
                .HasOne(x => x.Character)
                .WithOne(x => x.Identity)
                .HasForeignKey(x => x.YourFkKey);

CodePudding user response:

I higly recommend not to mix foreign and primary keys, since you can not gain anything with it, only problems

public class Identity {

        public long Id { get; set; }
        public string Tag { get; set; }

          public long CharacterId { get; set; }
          public virtual Character  Character { get; set; }
    }
    
    /// <summary>
    /// Represents an character ingame with all his attributes. 
    /// </summary>
    public class Character {

         public long Id { get; set; }
           public float Health { get; set; }

        public virtual Identity Identity { get; set; }
    }

or you can use this as well

public class Identity {

        public long Id { get; set; }
        public string Tag { get; set; }

        public virtual Character  Character { get; set; }
    }
    
    /// <summary>
    /// Represents an character ingame with all his attributes. 
    /// </summary>
    public class Character {

         public long Id { get; set; }
           public float Health { get; set; }

           public long IdentityId { get; set; }
        public virtual Identity Identity { get; set; }
    }

and the first plus is that if you use net5 you will not need any APIs in this case

  • Related