Reputation: 25
I am having troubles trying to figure out how to rename and configure an autogenerated table, from entity framework.
Here is my code:
public class ApplicationUser : IdentityUser
{
public virtual List<ApplicationUser> AddingFriends { get; set; }
public virtual List<ApplicationUser> AddedFriends { get; set; }
}
The result expressed by those entities once migrate to the database is as the following:
So I basically just want to rename this table and its column names. Besides that I also want to create a second table for blocked people which would have the same lists of entities. So basically when I add two other lists of application users it binds those property to this actual table shown below. Is there a way to control generated tables and configure them correctly?
Thanks in advance. Cheers.
Upvotes: 0
Views: 1068
Reputation: 25
After looking at the Fluent API Documentation i found that i could configure that with by adding those lines of code to OnModelCreating method:
modelBuilder.Entity<ApplicationUser>()
.HasMany(c => c.AddedFriends)
.WithMany(c => c.AddingFriends)
.Map(m =>
{
m.ToTable("Friends");
m.MapLeftKey("AddedUser");
m.MapRightKey("AddingUser");
});
modelBuilder.Entity<ApplicationUser>()
.HasMany(c => c.BloquedUsers)
.WithMany(c => c.BloquingUsers)
.Map(m =>
{
m.ToTable("Bloqueds");
m.MapLeftKey("BloquingUser");
m.MapRightKey("BloquedUser");
});
Thanks for your answers.
Upvotes: 0
Reputation:
You are using code first approach and this keeps track of the model and database table corresponding to that model.So, You cannot change the model.Options are to enable migration or to disable model checking.
check this link for migration
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application
Upvotes: 1