Harry
Harry

Reputation: 3205

Add ASP.Net Identity and Entities to same database

I created a new ASP.NET MVC project, I am using code-first. I have added ASP.NET Identity to this project enabled-migrations and can see the tables in my database on sql server express.

I have a "DefaultConnection" which points to my database on sqlserver like so:

 <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;database=worksmartDB;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Now, I am confused how to add my entities to the same database via code first? Below is my structure:

Models

 Applicant
 Template
MyProjectContext(which dervies from DBContext)


public class MyProjectContext: DbContext
{
    public MyProjectContext() : base("DefaultConnection")
    {
    }

public DbSet<Applicant> Applicants { get; set; }
public DbSet<Template> Templates { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    base.OnModelCreating(modelBuilder);
}

}

I try to enable migrations for this context but I receive the following error:

The context type 'MyProjectContext' was not found in the assembly 'MyProject'

I want to use same database for identity and entities.

Upvotes: 1

Views: 646

Answers (1)

Harry
Harry

Reputation: 3205

Well I got it to work, not sure if this is the correct method.

However,

When you add identity into a new project, you should do the following:

- Change public ApplicationDbContext()
        : base("YourConnection", throwIfV1Schema: false)
Note: this connection string should point to the same database you are going to use
- Run the application in browser
- Register a user (this will create the database for you, if one does not exist)

- Now Enable-Migrations
- Update-Database

And this is one way of adding identity into your project.

Upvotes: 2

Related Questions