LINQ to Entities does not recognize the method ‘System.String ToString()’ method in MVC 4

You got this error because Entity Framework does not know how to execute .ToString() method in sql. So you should load the data by using ToList and then translate into SelectListItem as: var query = dba.blob.ToList().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name_company, Selected = c.id.Equals(3) }); Edit: To make it more … Read more

MVC 5 Seed Users and Roles

Here is example of usual Seed approach: protected override void Seed(SecurityModule.DataContexts.IdentityDb context) { if (!context.Roles.Any(r => r.Name == “AppAdmin”)) { var store = new RoleStore<IdentityRole>(context); var manager = new RoleManager<IdentityRole>(store); var role = new IdentityRole { Name = “AppAdmin” }; manager.Create(role); } if (!context.Users.Any(u => u.UserName == “founder”)) { var store = new UserStore<ApplicationUser>(context); var … Read more

EF migration for changing data type of columns

You have a default constraint on your column. You need to first drop the constraint, then alter your column. public override void Up() { Sql(“ALTER TABLE dbo.Received DROP CONSTRAINT DF_Receiv_FromN__25869641”); AlterColumn(“dbo.Received”, “FromNo”, c => c.String()); AlterColumn(“dbo.Received”, “ToNo”, c => c.String()); AlterColumn(“dbo.Received”, “TicketNo”, c => c.String()); } You will probably have to drop the default constraints … Read more

Enable Migrations with Context in Separate Assembly?

This will only work in EF 6, but there was a release that added the -ContextProjectName parameter to the -enable-migrations command. By using this command you could do the following: enable-migrations -ContextProjectName MyProject.MVC -StartUpProjectName MyProject.MVC -ContextTypeName MyProject.MVC.MyContextFolder.MyContextName -ProjectName MyProject This will add migrations to your MyProject project using the context in the MyProject.MVC. You need … Read more