简体   繁体   中英

ASP.Net MVC 5 Doesn't work Database first method

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=&quot;
     data source=.\SQL2012;
     initial catalog=ETests;
     integrated security=True;
     MultipleActiveResultSets=True;
     App=EntityFramework&quot;"
     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; }
}

Are you sure you're using the same context? Typically, the UserManager uses it's own DbContext for ApplicationUser. Check the connection string for that DbContext.

This may help: Entity Framework 6 and Asp.Net Identity UserManager: Multiple DbContext

You will have to inherit the context class from IdentityDbContext. To do that you will have to change the t4 template file...

  1. Open Entities.tt file
  2. Navigate to the line ...

    <#=Accessibility.ForType(container)#> partial class Entities : DbContext

and update it to

<#=Accessibility.ForType(container)#> partial class Entities : IdentityDbContext<ApplicationUser>
  1. Save the file and check the generated context class. It should be...

    public partial class Entities : IdentityDbContext < ApplicationUser > { public Entities() : base("name=Entities") { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM