C
C#2y ago
pyrodistic

Entity Framework One-To-One [Answered]

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.
BlazeBin - rccwzntafwoj
A tool for sharing your source code with the world!
Relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
12 Replies
Angius
Angius2y ago
You need to define the relationship explicitly
builder.Entity<One>()
.HasOne(o => o.Two)
.WithOne(t => t.One);
builder.Entity<One>()
.HasOne(o => o.Two)
.WithOne(t => t.One);
Might need .HasForeignKey() as well
pyrodistic
pyrodistic2y ago
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);
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.
Angius
Angius2y ago
Configure only one side of the relationship, it's enough
pyrodistic
pyrodistic2y ago
Thank you, understood, but it continues with the same error (the first one).
Angius
Angius2y ago
What's your config now?
pyrodistic
pyrodistic2y ago
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);
}
}
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);
}
}
Angius
Angius2y ago
Still no .HasForeignKey<T>() I see The docs you linked talk about it as well
Angius
Angius2y ago
pyrodistic
pyrodistic2y ago
I might be wrong, please tell me but I thing the .HasForeignKey() makes it so it cannot be nullable, or is that completely incorrect?
Angius
Angius2y ago
That's incorrect .IsRequired() is what sets it Also, make the key nullable just to make sure So not public int VetId { get; set; } but public int? VetId { get; set; } for example
pyrodistic
pyrodistic2y ago
Shouldn't have assumed that .HasForeignKey() determined requirement. Thank you so much!
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Database - Relationship returns empty set even though records are present [Answered]I think I'm probably missing something stupid obvious... In my batch model, I have ```cs private reaSave storageFile to a folder on driveI have a storagefile ready to give to a control to crop an image the problem is that image crop wilDynamically load .dll into .NET 6 ProgramHi, I've a Blazor WebAssembly Hosted (.NET 6) web application. I've put some part of the applicationMove File To A Matching Type RefactoringWhen everything is top-level do you not need namespaces because everything is "top-level"? Thanks foClass making my program run as a background process after I close it [Answered]https://paste.myst.rs/7o8qz5vk After I close my program through RMB>Close window it continues runniHangfire - Unable to resolve services [Answered]Working with Hangfire. I have a plugin service which has an InitializeAsync method that looks like tASP.NET Website project without namespaces. Converting to ASP.NET Web application.Hi guys, I have a legacy ASP.NET Website project which I want to convert to ASP.NET Website applicatBest Way to Save Client_Secret and access_token?Hey i got now the Discord Login on my Tool Ready but now i need to Safe Data like the Access_token, How do I call a null event?```csharp public delegate void MyDel(); public event MyDel MyEvent; public void LeIdiomatic LINQ way to access both object and an object's property? [Answered]```cs var list = new List<string>(); foreach (Item item in items) { foreach (string thing in item