garma
garma

Reputation: 213

MVC3: EF Code First and authentication

I'm building a MVC3 EF Code First application on an SQLExpress DB. It is very important to me that databases are auto-generated whenever the schema changes or something like that. Now I am adding authentication to my application, and I looked into the Authentication framework that comes with MVC. However, as it appears it requires me to run aspnet_regsql.exe every time a new database is generated. I really need the Authentication tables to be in the same database as my application database.

I'm basically wondering what my options are;

I could use some pointers in the right direction.

Upvotes: 4

Views: 2761

Answers (2)

Chris Foster
Chris Foster

Reputation: 1300

You can override the seed method of your DbMigrationsConfiguration or create a migration.

You should then be able to execute the SQL that can be generated from aspnet_regsql.exe or execute aspnet_regsql.exe providing you have permission.

UPDATE 1:

I ended up doing as follows:

Add-Migration AspNetRegSql

public partial class AspNetRegSql : DbMigration
{
    public override void Up()
    {
        Migrate(false);
    }

    public override void Down()
    {
        Migrate(true);
    }

    private static void Migrate(bool down)
    {
        var action = down ? "R" : "A";
        var connectionstring = ConfigurationManager.ConnectionStrings["DataContext"];
        var arguments = string.Format(@"-Q -C ""{0}"" -{1} mrp", connectionstring, action);

        var processStartInfo = new ProcessStartInfo
        {
            WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace(@"file:\", string.Empty),
            FileName = "aspnet_regsql.exe",
            Arguments = arguments,
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden
        };

        using (var process = Process.Start(processStartInfo))
        {
            process.WaitForExit();
        }
    }
}

Then added "aspnet_regsql.exe" to my project and changed build action to "Copy if newer".

UPDATE 2:

I was not aware of the library that NSGaga posted - I have not looked at it but would recommend you also consider it.

UPDATE 3:

Some useful links:

Upvotes: 3

NSGaga
NSGaga

Reputation: 14302

You could start with CodeFirst Membership Provider - which is a custom provider based on code first - and expand on that if needed (also read through the recommended use on the site).

The above is IMO the best way as you can really integrate the two (and e.g. in case you might need to access e.g. the membership tables directly, relate to your own user/client tables etc.).

But also you have other options...

a) writing an own Db initializer and Seed-ing the database (like what's suggested here https://stackoverflow.com/a/5462981/417747. note: you can utilize different approaches, from running aspnet tables SQL script to creating model for aspnet tables etc.) or

b) use Db initializer which 'preserves' the database - and prepare the database first using aspnet tables

c) (last but not least) use migrations (as rightly suggested by @Chris). Migrations are pretty powerful. You could run Update-Database -Script to generate SQL instead of changing the Db - then combine that (with aspnet script or seed-ed db) and use it for creating or updating the db. Migrations require some learning but again is well worth the effort for anything more complex.

hope this helps

Upvotes: 3

Related Questions