I guess withmany and withoptional both will be wrong
modelBuilder.Entity<Student>().HasRequired(c =>
c.Studentaccount).WithMany(s => s.student).HasForeignKey(s => s.StudentId);
HasOptional
modelBuilder.Entity<Student>().HasRequired(c => c.Studentaccount).HasOptional(s => s.student);
For all the relationship in EF you can have a look to the official MSDN page
https://msdn.microsoft.com/en-us/data/jj591620.aspx
Related
I am migrating my project from Entity Framework to EF Core 6 but now I am stuck at this point.
modelBuilder.Entity<tblStoreGroup>()
.HasMany(e => e.tblUsers)
.WithMany(e => e.tblStoreGroups)
.Map(m => m.ToTable("tblBridgeUserGroup")
.MapLeftKey("GroupID")
.MapRightKey("username"));
Because the Map() property is not available in EF Core, can someone please provide a solution for this code?
I am a beginner in Entity Framework.
Thanks in advance
You can try using UsingEntity:
modelBuilder.Entity<tblStoreGroup>()
.HasMany(p => p.tblUsers)
.WithMany(p => p.tblStoreGroups)
.UsingEntity<Dictionary<string, object>>(
"tblBridgeUserGroup",
j => j
.HasOne<tblUser>()
.WithMany()
.HasForeignKey("username"),
j => j
.HasOne<tblStoreGroup>()
.WithMany()
.HasForeignKey("GroupID"));
See Joining relationships configuration section of the docs.
I'm learning ASP.NET Core Web API and Entity Framework Core. Currently, I'm stuck figuring out how to populate the foreign keys with the actual values, as shown below.
Current Result:
Desired Result:
fkSecuritySecurityTypeId and fkSecurityExchangeId are populated.
DatabaseContext.cs:
The DatabaseContext class was built using dotnet ef dbcontext scaffold. Here's a portion of it:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Security>(entity =>
{
entity.HasOne(d => d.FkSecurityExchange)
.WithMany(p => p.Securities)
.HasForeignKey(d => d.FkSecurityExchangeId)
.OnDelete(DeleteBehavior.ClientSetNull);
entity.HasOne(d => d.FkSecuritySecurityType)
.WithMany(p => p.Securities)
.HasForeignKey(d => d.FkSecuritySecurityTypeId)
.OnDelete(DeleteBehavior.ClientSetNull);
});
Research:
AutoInclude() seems like the solution. I'm having a hard time understanding how to use AutoInclude() in my code since obviously my code is a bit different from how the docs use AutoInclude.
Questions:
Is AutoInclude() the correct approach ?
If so, how should it be used in this case ?
Thanks for your help.
This question already has an answer here:
EF Core Include on multiple sub-level collections
(1 answer)
Closed 3 years ago.
I am trying to query an entity with multiple levels of collections, and multiple collections at a single level. I'm using Include() and ThenInclude(), but not having much success. The examples I find don't have multiple collections on the same level and I haven't had any luck applying the technique to my use case.
This is a simplified illustration of my entities. Those with the [] are collections:
Home
Areas[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
I've gotten this far:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
However, this misses the Categories[].Recommendations[] collection. It's because there are two collections at the same level (Recommendations[] and Subcategories[]).
Any suggestions for a way to structure this query so I can get the desired result?
Thanks.
You have to call Include for each level:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(a => a.Recommendations)
You have to write your query as follows:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(c => c.Subcategories)
I'm trying to do an include of a nested entity. I assumed the below code would work but it does not recognize the CapitalMailOrders entitycollection. Can anyone point me in the right direction to include these?
var result = db.Contacts
.Include(a => a.IDXPageLinks)
.Include(b => b.ReboGatewayLoginInfoes)
.Include(c => c.SocialMedias)
.Include(d => d.WebSiteInfoes)
.Include(e => e.ContactImages)
.Include(f => f.RealtorSetUpProcesses)
.Include(h => h.RealtorSetUpProcesses.CapitalMailOrders) // getting compile time error here. Doesn't recognize Entity
.Include(g => g.Contact_CarrierCode_Assignments)
.FirstOrDefault(c => c.ContactID == id);
Thanks
The extra entity level is accessed from a collection and so you need to add a Select in order to bring all the entries into the include.
.Include(h => h.RealtorSetUpProcesses.Select(h2 => h2.CapitalMailOrders)
I can't seem to find how to eager load multiple child child collections in EF. So I can do:
blawConext.Blaws
.Include(b => b.ChildCollection
.Select(cc => cc.ChildChildCollection)
)
I can even go deeper and deeper without a problem but I can't get the umm peer? collection, The below does not work
blawConext.Blaws
.Include(b => b.ChildCollection
.Select(cc => cc.ChildChildCollection1)
.Select(cc => cc.ChildChildCollection2)
)
You can specify multiple includes:
blawConext.Blaws
.Include(b => b.ChildCollection.Select(cc => cc.ChildChildCollection1))
.Include(b => b.ChildCollection.Select(cc => cc.ChildChildCollection2))