Problem z połaczeniem do bazy danych

0

Witam, problem polega na tym że napisałem klasy, czyli stworzyłem model bazy danych. Następnie zrobiłem InitMigration, która utworzyła bazę danych, a później z poziomu kodu programu utworzyłem rekordy w bazie danych. Nie mam zielonego pojęcia czemu to przestało działać a nie zmieniałem nic ani connectionstringa ani contextu. Program wywala taki Exception:

System.Data.SqlClient.SqlException (0x80131904): Wystąpił błąd związany z siecią lub wystąpieniem podczas ustanawiania połączenia z serwerem programu SQL Server. Nie można odnaleźć serwera lub jest on niedostępny. Sprawdź, czy nazwa wystąpienia jest poprawna i czy konfiguracja serwera programu SQL Server zezwala na połączenia zdalne. (provider: SQL Network Interfaces, error: 50 - Wystąpił błąd programu Local Database Runtime. Specified LocalDB instance name is invalid.
)
w System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
w System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
w System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
w System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
w System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
w System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
w System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) w System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
w System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) w System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
w System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) w System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
w System.Data.SqlClient.SqlConnection.Open()
w System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
w System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action2 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed)
w System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
w System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()
w System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
w System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) w System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) w System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action1 act)
w System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action1 act) w System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
w System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection) w System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection)
w System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
w System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
w System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
w System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
w System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func3 createMigrator, ObjectContext objectContext) w System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) w System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) w System.Data.Entity.CreateDatabaseIfNotExists1.InitializeDatabase(TContext context)
w System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf1.<CreateInitializationAction>b__e() w System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) w System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() w System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) w System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input)
w System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 action) w System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() w System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) w System.Data.Entity.Internal.Linq.InternalSet1.Initialize()
w System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() w System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider()
w System.Linq.Queryable.Select[TSource,TResult](IQueryable1 source, Expression1 selector)
w PRSS.Program.Main(String[] args) w C:\Users\Marcin\Source\Repos\prss\PRSS\PRSS\Program.cs:wiersz 42
ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1983577829,State:0,Class:20

ConnectionString wygląda tak:

  <connectionStrings>
    <add name="RSSContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=RssDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Oraz Context

public class RSSContext : DbContext
    {
        public RSSContext() : base("RSSContext")
        {

        }
        public DbSet<RSSCategory> RSSCategories { get; set; }
        public DbSet<RSSChannel> RSSChannels { get; set; }
        public DbSet<RSSFeed> RSSFeeds { get; set; }
    }

Mam nadzieję że ktoś się już spotkał z takim problem i się podzieli rozwiązaniem.

0

Coś mi się kojarzy, że musi to być tak:

public RSSContext() : base("name=RSSContext")
0

Sprawdź, czy masz włączonego SQLa. U mnie się czasem z jakiegoś powodu serwer SQL nie załącza i muszę go ręcznie uruchomić w configuration tools.
Błąd mówi, że nie można znaleźć serwera. A lokalnego serwera nie znajdziesz wtedy, kiedy nie jest on uruchomiony. Więc sprawdź to.

0

Jest włączony. Jak wejdę w server explorer i dam "add database" a następnie w "Server name" wpisze (localdb)\mssqllocaldb i niżej rozwinę listę to bazy się pojawiają oraz jak nacisnę test connection to też wyświetla że jest połączenie z bazą.

Jedno mnie zastanawia skoro Connectionstring jest taki:

 <connectionStrings>
    <add name="RSSContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=RssDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

To dlaczego bazę utworzyło w bazie "master" a nie w utworzyło nowej bazy o nazwie RssDB? Może to właśnie tu należy szukać rozwiązania problemu?

title

0

W connection stringu próbujesz łączyć się z nieistniejącą bazą. Widocznie tabele kiedyś utworzyłeś w bazie Master i potem myślałeś, że jednak utworzyłeś bazę RssDB. A jej nie ma. Utwórz bazę RssDB wraz z wszystkimi jej tabelami i sprawdź.

Pierwszą rzeczą jaką musisz zrobić, to będąc zalogowanym na bazie master, utworzyć swoją docelową bazę. Następnie PRZEŁĄCZYĆ się na docelową bazę i wykonywać tam wszystkie zapytania. Ale to jest rola instalatora. Więc pewnie po prostu uruchomiłeś skrypty w Management Studio tyle że na bazie master.

0

Nie, nie. Zrobiłem model a następnie za pomocą mechanizmu migracji utworzyłem bazę z podanym wcześniej connectionstringiem.

0

OK, to czemu tej bazy nie ma u Ciebie na serwerze? Na zrzucie jej nie ma.

0

Ok, już wszystko smiga. Zmieniłem connectionstring na:

<connectionStrings>
    <add name="RSSContext" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=RssDB;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|\RssDB.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

oraz zrobiłem w mechanizmie migracji Update-database, i stworzyło mi nową bazę wraz z osobnym katalogiem RssDB.

1 użytkowników online, w tym zalogowanych: 0, gości: 1