I am developing Asp.Net MVC 5 application with Entity Framework 6. I am using DatabaseFirst method and I want to use standard Microsoft.AspNet.Identity
library for user authentication.
1). I have created Asp.Net MVC 5 project.
2). Changed DefaultConnection to my local SQL server.
3). Run application to generate database on the server.
4). Added my new tables in this new database.
5). Generated model from data base.
When I am trying to get some data from my tables, I am getting an error message:
// GET: Tests
public ActionResult Index()
{
return View(_repository.GetAllTests());
}
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
Ok, I changed my connection string from:
<add name="Entities" connectionString="Data Source=.\SQL2012;
Initial Catalog=ETests;
Integrated Security=True"
providerName="System.Data.SqlClient" />
to
<add name="Entities"
connectionString="metadata=res://*/Entities.csdl|
res://*/Entities.ssdl|
res://*/Entities.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=.\SQL2012;
initial catalog=ETests;
integrated security=True;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
Now I can get data from my tables, but I can't to use standart user authentication. I am getting error:
The entity type ApplicationUser is not part of the model for the current context.
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); // In this line
So, What I'am doing wrong? How to write correct connection string?
UPDATE: I have standart DbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("Entities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
and context auto-generated with model:
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
public virtual DbSet<Document> Documents { get; set; }
public virtual DbSet<Image> Images { get; set; }
public virtual DbSet<Option> Options { get; set; }
public virtual DbSet<Paragraph> Paragraphs { get; set; }
public virtual DbSet<Question> Questions { get; set; }
public virtual DbSet<Section> Sections { get; set; }
public virtual DbSet<Test> Tests { get; set; }
}