Β© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#β€’11mo agoβ€’
26 replies
~𝓒π“ͺ𝓻𝓲𝓾𝓼~

Entity Framework Entity Saving

hi guys can someone help me with EF?
I have a Visitor and want to add a plannedVisit to it

public sealed class Visitor : Entity<Guid>
{
public ICollection<PlannedVisit> PlannedVisits { get; set; } = new List<PlannedVisit>();
}
public sealed class PlannedVisit : Entity<Guid>
{
public Guid VisitorId { get; set; }
public Visitor Visitor { get; set; }
}

Configuration
builder.HasMany(visitor => visitor.PlannedVisits)
.WithOne(plannedVisit => plannedVisit.Visitor)
.HasForeignKey(plannedVisit => plannedVisit.VisitorId);

Can i somehow add the plannedVisit without calling _context.PlannedVisits.Add(plannedVisit) to the Changetracker / will the changetracker somehow pick it up?

Would be nice if i could just do it like this, but here i alwais get an error

public Result<PlannedVisit> PlanVisit(DateTime plannedArrival, DateTime? plannedDeparture, VisitLocation location, List<BriefingType> requiredBriefings, VisitGroup group)
{
var result = PlannedVisit.Create(
this,
plannedArrival,
plannedDeparture,
location,
group,
requiredBriefings);

if (result.Failed)
{
return result.Error;
}

PlannedVisits.Add(result.Value);

return result.Value;
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Entity framework
C#CC# / help
14mo ago
❔ Entity Framework
C#CC# / help
3y ago
❔ Entity Framework
C#CC# / help
4y ago
Entity Framework - MODEL and ENTITY
C#CC# / help
4y ago