I am writing an application on ASP.NET MVC.

It took a list of all registered users. To display the list, I decided to use Membership.GetAllUsers() , but I get an exception:

There was a network or instance error while connecting to SQL Server. Server not found or unavailable. Ensure that the instance name is correct and that remote connections are allowed on SQL Server. (provider: SQL Network Interfaces, error: 26 - Error finding the specified server or instance)

Tell me what code is needed? Database localdb , trying to get a list of users through Membership.GetAllUsers() in the controller and pass to the form. There are tables in the database, the connection is normal if you connect via the DatabaseContext db = new DatabaseContext() object DatabaseContext db = new DatabaseContext() . But in this way I can not get the role of users.

 <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-SoftwareDevelopmentCustom;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-SoftwareDevelopmentCustom.mdf" providerName="System.Data.SqlClient" /> </connectionStrings> 

Code:

  using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Threading; using System.Web.Mvc; using WebMatrix.WebData; using SoftwareDevelopmentCustom.Models; using System.Web.Security; namespace SoftwareDevelopmentCustom.Filters { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute { private static SimpleMembershipInitializer _initializer; private static object _initializerLock = new object(); private static bool _isInitialized; public override void OnActionExecuting(ActionExecutingContext filterContext) { // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ ASP.NET Simple Membership происходит ΠΎΠ΄ΠΈΠ½ Ρ€Π°Π· ΠΏΡ€ΠΈ стартС прилоТСния LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); } private class SimpleMembershipInitializer // ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ класса ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ‚ΠΎΡ€Π° { public SimpleMembershipInitializer() { Database.SetInitializer<DatabaseContext>(null); try { using (var context = new DatabaseContext()) { if (!context.Database.Exists()) { // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π±Π°Π·Ρ‹ Π΄Π°Π½Π½Ρ‹Ρ… SimpleMembership Π±Π΅Π· примСнСния ΠΌΠΈΠ³Ρ€Π°Ρ†ΠΈΠΈ Entity Framework ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } // Настройка ASP.NET Simple Membership // 1 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ - имя строки ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ ΠΊ Π±Π°Π·Π΅ Π΄Π°Π½Π½Ρ‹Ρ…. // 2 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ - Ρ‚Π°Π±Π»ΠΈΡ†Π°, которая содСрТит ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡΡ… // 3 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ - имя ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Π² Ρ‚Π°Π±Π»ΠΈΡ†Π΅, которая ΠΎΡ‚Π²Π΅Ρ‡Π°Π΅Ρ‚ Π·Π° Ρ…Ρ€Π°Π½Π΅Π½ΠΈΠ΅ Π»ΠΎΠ³ΠΈΠ½Π° // 4 ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ - autoCreateTables автоматичСскоС созданиС Ρ‚Π°Π±Π»ΠΈΡ† Ссли ΠΎΠ½ΠΈ Π½Π΅ ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‚ Π² Π±Π°Π·Π΅ WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); SimpleRoleProvider roles = (SimpleRoleProvider)Roles.Provider; SimpleMembershipProvider membership = (SimpleMembershipProvider)Membership.Provider; // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° наличия Ρ€ΠΎΠ»ΠΈ Admin if (!roles.RoleExists("Admin")) { roles.CreateRole("Admin"); } // Поиск ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ с Π»ΠΎΠ³ΠΈΠ½ΠΎΠΌ admin if (membership.GetUser("admin", false) == null) { membership.CreateUserAndAccount("admin", "qwe123"); // созданиС ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ roles.AddUsersToRoles(new[] { "admin" }, new[] { "Admin" }); // установка Ρ€ΠΎΠ»ΠΈ для ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ } } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } } } } } 
  • And how should we debug it without seeing the code and the ConnectionString? - Interface Unknown
  • More inclined that connectionstrings is to blame for everything. At least try to reconnect. - Adam
  • Deal with the problem? - Denis Bubnov

1 answer 1

The problem is not in the code, the problem is in the connection string.

 using (var context = new DatabaseContext()) { } 

In fact, this code does not create a connection. The connection is made at the time of the first request, i.e. you have it

 if (!context.Database.Exists()){} 

Try to connect to the database that is specified in your DefaultConnection from Visual Studio Server Explorer.

    Protected by a community spirit ♦ 4 Aug '15 at 14:01 .

    Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

    Maybe you want to answer one of the unanswered questions ?