Entity Framework Map() property alternate in EF Core 6 - entity-framework

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.

Related

Load Related Data

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.

Error upgrating to Entity Framework Core 3: RelationalReferenceCollectionBuilderExtensions does not exist in the current context

Im upgrating from Entity Framework Core 2 to Entity Framework Core 3. And I have this error:
Error CS0103 The name 'RelationalReferenceCollectionBuilderExtensions'
does not exist in the current context
The error is on method OnModelCreating(ModelBuilder modelBuilder), this is the code:
modelBuilder.Entity<Person>(entity =>
{
//some props
RelationalReferenceCollectionBuilderExtensions.HasConstraintName((ReferenceCollectionBuilder)entity
.HasOne(d => d.RuleCodeNavigation)
.WithMany(p => p.Person)
.HasForeignKey(d => d.RuleCode)
.OnDelete(DeleteBehavior.ClientSetNull), "FK_Person_Rules");
});
It seems the class RelationalReferenceCollectionBuilderExtensions does not exist in EF Core 3.
Any idea how I can fix this?
Not sure what happened to RelationalReferenceCollectionBuilderExtensions, but the suggested method to name your constraints is this
modelBuilder.Entity<Person>(entity =>
{
//some props
entity
.HasOne(d => d.RuleCodeNavigation)
.WithMany(p => p.Person)
.HasForeignKey(d => d.RuleCode)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Person_Rules");
});

for one to one relationship, which option should i select?

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

multiple tables mapping entity framework code first

I have 3 Entities Roles, Modules and Permissions and I am following code first approach using EF 6
Roles has RoleID , Modules has ModuleID and Permissions has PermissionID as Keys.
Now I want Map these 3 ID values to another mapping table RoleAccess on creating Role
I am able to map entity to with ICollection property and the same is not working for the above scenario.
help me out with code suggestions...
The configuration i am trying add is below..
'this is the mapping configuration i am trying `this.ToTable("Roles");
//this.HasKey(c => c.Id)
// .HasMany(c => c.Permissions)
// .WithRequired()
// .HasForeignKey(c => new { c.SubModuleId, c.EntitlementTypeId });
this.HasMany(a => new[] {a.SubModules,a.EntitlementTypes})
.WithMany(b => b.Roles)
.Map(mc =>
{
mc.ToTable("Permissions");
mc.MapLeftKey("RoleId");
mc.MapRightKey(new[] { "SubModuleId", "EntitlementTypeId" });
});`'

Linq To Sql Entity Framework Getting Nested Entities

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)