I'm trying to add many-to-many recursive relation within given entity in entity framework. My problem is that I can't add additional properties in the table "MyEntityTable". Here is the code snippet:
modelBuilder.Entity<MyEntity>()
.HasMany(a => a.ParentTransportUnits)
.WithMany(b => b.ChildTransportUnits)
.Map(mc => { mc.ToTable("MyEntityTable");
mc.MapLeftKey("ParentColumn");
mc.MapRightKey("ChildColumn"); });
Any suggestion is appreciated!
Thanks in advance
Related
I would like to sort a tables child table and then sort the children of the child table.
Is there anyway to do this in a nice way with eager loading?
This does not work and sorts actually nothing.
await _dbContext.MainTable
.Include(m => m.Chapter.OrderBy(c => c.SortingOrder))
.ThenInclude(c => c.Paragraph.OrderBy(p => p.SortingOrder))
.FirstOrDefaultAsync(m => m.Id == Id);
You will need to select and then perform the shorting. Shorting will not be done at the time of include.
In the RC1 of EntityFramework 7, released yesterday, Cascade Delete was added.
To disable it per relationship, I can use :
builder.Entity<Site>().HasOne(e => e.Person)
.WithMany(x => x.Sites).Metadata.DeleteBehavior = DeleteBehavior.Restrict;
I want to disable it globally for a DbContext, but I didn't find a way. How can I do ?
Someone stated on the github project forum that the only way to do it right now is to iterate through all relationships in the method OnModelCreating(ModelBuilder builder), and set the DeleteBehavior property to DeleteBehavior.Restrict :
foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
Right now conventions are not configurable. The current CascadeDelete convention only applies to required relationships. Relationships Conventions: Cascade Delete on efproject.net (Official EF7 docs) You could disable the required relationship explicitly if you understand well the consequences.
modelBuilder.Entity<Site>()
.HasOne(p => p.Person)
.WithMany(b => b.Sites)
.IsRequired(false);
Otherwise (and recommended), you need to set the On Delete behavior explicitly ( as you already discovered).
modelBuilder.Entity<Site>()
.HasOne(p => p.Person)
.WithMany(b => b.Sites)
.OnDelete(DeleteBehavior.Restrict);
If you all need more details I will send them for sure. Here is a screen shot containing 2 tables:
Basically this is what looks like in SQL today:
I am trying to use the fluent API for the relationship but not sure how to do it.
Table 1 has a PK and a FK.
Table 2 does not have a PK, only FK.
Below is an example of what I need but this code applies to a different set of tables. I am trying to get the "relationship" syntax correct for the scenario described here:
this.ToTable("Server");
//primary key
this.HasKey(t => t.serverId);
//properties
...
//relationships
this.HasMany(n => n.NetworkAdapters)
.WithRequired(s => s.Server)
.HasForeignKey(s => s.serverId)
.WillCascadeOnDelete(true);
Thank you
Without the primary key on table 2 you are most likely looking for a One-to–Zero-or-One relationship. You might possibly be requiring both ends which I will describe too.
One-to–Zero-or-One
// Configure the primary key for Table1
modelBuilder.Entity<Table1>()
.HasKey(t => t.networkAdapterId);
// Map one-to-zero or one relationship
modelBuilder.Entity<Table2>()
.HasRequired(t => t.Table1)
.WithOptional(t => t.Table2);
One-to-One
// Configure the primary key for the Table1
modelBuilder.Entity<Table1>()
.HasKey(t => t.networkAdapterId);
// Map one-to-one relationship
modelBuilder.Entity<Table2>()
.HasRequired(t => t.Table1)
.WithRequiredPrincipal(t => t.Table2);
See here for more details
When I create two entities with many to many relationship, it will generate a relationship table in the database, is it possible to specify the table's name?
Yes but you have to use fluent API:
mb.Entity<FirstEntity>()
.HasMany(a => a.SecondEntities)
.WithMany(b => b.FirstEntities)
.Map(mc =>
{
mc.ToTable("YourTableName", "YourDbSchema");
mc.MapLeftKey("FirstEntityKeyColumnName");
mc.MapRightKey("SecondEntityKeyColumnName");
});
In my code I have a many to many relationship defined using:
modelBuilder.Entity<Post>()
.HasMany( p => p.Authors ).WithMany();
Post.Authors is an ICollection of User entities.
The ModelBuilder automatically creates a table called PostUsers.
How can I override the table naming convention so that the ModelBuilder names the table PostAuthors when the database is created from the model?
Thanks!
You can use:
modelBuilder.Entity<Post>
.HasMany(p => p.Authors)
.WithMany()
.Map(m => m.ToTable("PostAuthors", "dbo"));