Entity Framework One-To-One [Answered]

Ppyrodistic8/17/2022
Code at: https://paste.mod.gg/rccwzntafwoj/0

When attempting to change the database I'm getting:

The dependent side could not be determined for the one-to-one relationship between 'Room.Vet' and 'Vet.Room'. To identify the dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship, configure them independently via separate method chains in 'OnModelCreating'. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.

OR

The property 'Room.Vet' is of type 'Vet' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

I wanted to be able to create a Room that can have 0 or 1 Vets, and create a Vet that has 0 or 1 Rooms.

What am I doing incorrectly?

Thanks in advance.
AAngius8/17/2022
You need to define the relationship explicitly
AAngius8/17/2022
builder.Entity<One>()
    .HasOne(o => o.Two)
    .WithOne(t => t.One);
AAngius8/17/2022
Might need .HasForeignKey() as well
Ppyrodistic8/17/2022
I was currently doing:
            modelBuilder.Entity<Vet>()
                .HasOne(s => s.Room).WithOne(s => s.Vet)
                .IsRequired(false);

            modelBuilder.Entity<Room>()
                .HasOne(s => s.Vet).WithOne(s => s.Room)
                .IsRequired(false);

Which prompts the second error when trying to submit changes.
AAngius8/17/2022
Configure only one side of the relationship, it's enough
Ppyrodistic8/17/2022
Thank you, understood, but it continues with the same error (the first one).
AAngius8/17/2022
What's your config now?
Ppyrodistic8/17/2022
Same that's on the link for code so:
public class DataContext : IdentityDbContext<User>
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options)
        {
        }

        public DbSet<Room> Rooms { get; set; }
        public DbSet<Vet> Vets { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Vet>()
                .HasOne(s => s.Room).WithOne(s => s.Vet)
                .IsRequired(false);
        }
    }
AAngius8/17/2022
Still no .HasForeignKey<T>() I see
AAngius8/17/2022
The docs you linked talk about it as well
AAngius8/17/2022
Image
Ppyrodistic8/17/2022
I might be wrong, please tell me but I thing the .HasForeignKey() makes it so it cannot be nullable, or is that completely incorrect?
AAngius8/17/2022
That's incorrect
AAngius8/17/2022
.IsRequired() is what sets it
AAngius8/17/2022
Also, make the key nullable just to make sure
AAngius8/17/2022
So not public int VetId { get; set; } but public int? VetId { get; set; } for example
Ppyrodistic8/17/2022
Shouldn't have assumed that .HasForeignKey() determined requirement. Thank you so much!
AAccord8/18/2022
✅ This post has been marked as answered!