i use code first in mvc5. After building
the database tables ,
at the End of
table name, letter s
is added .
for example :
name of model : book
name of table : books
why?
Hello,
Try adding this to your entities dbcontext class, and then recreate your database
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>(); }
but after update-database , show this error :
One or more validation errors were detected during model generation:
Project.Models.IdentityUserLogin: : EntityType ‘IdentityUserLogin’ has no key defined. Define the key for this EntityType.
Project.Models.IdentityUserRole: : EntityType ‘IdentityUserRole’ has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet ‘IdentityUserLogins’ is based on type ‘IdentityUserLogin’ that has no keys defined.
IdentityUserRoles: EntityType: EntitySet ‘IdentityUserRoles’ is based on type ‘IdentityUserRole’ that has no keys defined.
Hi Neda99,
Thanks for your post.
According to your post.
You use code-First in mvc.Based on my knowledge,you add this into the model like this:
public class sampleDBContext : DbContext { public DbSet<book> books{ get; set; } }
As far as I know,The sampleDBContext class you created handles the task of connecting to the database and mapping book objects to database records. One question you might ask, though, is how to specify which database it will connect to. You’ll do that
by adding connection information in the Web.config file of the application
Entity Framework Code First detected that the database connection string that was provided pointed to a books database that didn’t exist yet, so Code First created the database automatically. You can verify that it’s been created by looking in the
App_Data folder. If you don’t see the Movies.mdf file, click the
Show All Files button in the Solution Explorer toolbar, click the
Refresh button, and then expand the App_Data folder.
Notice how the schema of the books table maps to the book class you created earlier. Entity Framework Code First automatically created this schema for you based on your book
class.
In details:
Hope this helps you.
Best Regards,
Eileen
thanks
I have no problem in creating database
My question is:
After building the database tables , at the End of table
name, letter s is added .
for example :
name of model : book
name of table in database : books
public class sampleDBContext : DbContext { public DbSet<book> book{ get; set; } }
why?
Hi Neda99,
Thanks for your reply.
If you didn’t want to add "S" in the name of the table in the database,you can add this
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Configure Code First to ignore PluralizingTableName convention // If you keep this convention, the generated tables // will have pluralized names. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); }
More information:
#Custom Code First Conventions (EF6 onwards)
http://msdn.microsoft.com/en-us/data/jj819164.aspx
Hope this helps you.
Best Regards,
Eileen
hi Eileen
thanks
error after update-database:
One or more validation errors were detected during model generation: Project.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. Project.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
IdentityModels.cs:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("nedaConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Configure Code First to ignore PluralizingTableName convention // If you keep this convention, the generated tables // will have pluralized names. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } public System.Data.Entity.DbSet<Project.Models.Config> Config { get; set; } public System.Data.Entity.DbSet<Project.Models.Log> Log { get; set; } public System.Data.Entity.DbSet<Project.Models.city> city{ get; set; } }
Hi Neda99,
Thanks for your reply.
You can try add the code below into OnModelCreating:
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
Hope this helps you.
Best Regards,
Eileen
hi Eileen
thanks
error after update-database:
Cannot find the object "dbo.AspNetUserClaims" because it does not exist or you do not have permissions.
IdentityModels:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("nedaConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Configure Code First to ignore PluralizingTableName convention // If you keep this convention, the generated tables // will have pluralized names. modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); }
You have to disable pluralization if you dont want this nature.
try to override OnModelCreating method of DbContext class.
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); }
Thanks it worked for me!!