c# - The name 'Roles' does not exist in the current context -
i have ported migrations
web app class library project. works fine, except can't call static class roles
.
i have included namespace using system.web.security;
roles
located.
here configuration.cs file contents:
namespace _datacontext.migrations { using system; using system.data.entity; using system.data.entity.migrations; using system.linq; using webmatrix.webdata; using system.web.security; internal sealed class configuration : dbmigrationsconfiguration<_datacontext.datacontext> { public configuration() { automaticmigrationsenabled = true; } protected override void seed(_datacontext.datacontext context) { // method called after migrating latest version. // can use dbset<t>.addorupdate() helper extension method // avoid creating duplicate seed data. e.g. // // context.people.addorupdate( // p => p.fullname, // new person { fullname = "andrew peters" }, // new person { fullname = "brice lambson" }, // new person { fullname = "rowan miller" } // ); // seedmembership(); } private void seedmembership() { websecurity.initializedatabaseconnection("defaultconnection", "userprofile", "userid", "username", autocreatetables: true); // doesn't work either: //var roles = (simpleroleprovider)roles.provider; //var membership = (simplemembershipprovider)membership.provider; if (roles.roleexists("administrator")) roles.createrole("administrator"); } } }
error message is:
the name 'roles' not exist in current context
what missing here?
[edit]
i have been doing more research , appears have create object simpleroleprovider
in order access roleexists
method.
but why have way? why can't use:
if (roles.roleexists("administrator")) roles.createrole("administrator");
roles
comes static class
?
you should able access roles directly, not recommend when using simplemembership providers. being said, have assembly system.web referenced in project?
the preferred method getting role provider this:
var roles = (webmatrix.webdata.simpleroleprovider)roles.provider; if (!roles.roleexists("admin")) { roles.createrole("admin"); }
if compare definition of roles versus simpleroleprovider see there quite bit of difference. looks simpleroleprovider not implement complete interface roles, not required when implementing custom provider. may "not implemented" exception on methods if call them directly roles. simpleroleprovider provides additional methods/properties can useful when using simplemembership.
Comments
Post a Comment