Entity Framework 4.1 RC: Override name of Many to Many join table - entity-framework

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"));

Related

EF7 RC1 : Disable Cascade Delete

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);

Many-to-many recursive relation

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

Need proper syntax using EF6 Fluent API between 2 tables using PK and FK

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

How to specify relationship name in code-first many to many relationship

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");
});

How to modify mapping name in many-to-many assossiation

When I create an many to many assossiation on two entities, there will be a relationship table created contains the two entities' primary key in the database. But the name of the column seems no sence, how to modify them?
eg.
an Article table and a Blog table, the relationship table will contains two columns' like Article_ArticleID and Blog_BlogID
I want to modify the name at mapping window, but I find it read only, anyone can help?
Thanks in advance!
you can use:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasMany(b => b.Articles)
.WithMany(a => a.Blogs)
.Map
(
m =>
{
m.MapLeftKey("BlogID");
m.MapRightKey("AritcleID");
m.ToTable("BlogArticle");
}
);
base.OnModelCreating(modelBuilder);
}