Reputation: 5056
I have an MVC5 project with Individual user account authentication. I have make a test registered myself and everythings works. Then for test sake I've added a property to ApplicationUser
class, now ti looks like this:
public class ApplicationUser : IdentityUser
{
public string NewPropery { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Then I've opened the Package Manager Console in Visual Studio 2013 and run:
PM> Enable-Migrations
PM> Add-Migration "NewProperty"
PM> Update-Database
first two commands goes well but fail on the third command and this is the error:
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
The database is the usual LocalDb created by MVC template, I leave the connection string in the project as it was created:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-NyProject.Web-20160325120840.mdf;Initial Catalog=aspnet-MyProject.Web-20160325120840;Integrated Security=True"
providerName="System.Data.SqlClient" />
Where is the error?
UPDATE 1 - After Steve comment
I use to select the project in Package Manager Console, making the project a "Startup project" this will produce a different error There is already an object named 'AspNetRoles' in the database.
.
This is true. Should I remove pre-existent tables, and what about my data?
Upvotes: 0
Views: 1412
Reputation: 12304
You are getting that error because you have not established a baseline initial migration so EF thinks all your objects need to be created. This is because it builds models based on the code and compares it to the snapshot stored in the last migration, so try this:
1) Comment out the new field.
2) Delete the existing migration.
3) Add a baseline migration: add-migration Initial -IgnoreChanges
4) update-database (now EF has a snapshot to compare changes to).
5) add-migration NewProperty
6) update-database
Upvotes: 1