2017-05-14 6 views
0

VS2017がインストールされたAzureで仮想マシンを作成しました。次に、私は、Entity Frameworkを使ってデータベースに要素を追加する作業を行っているプロジェクトをクローンしました。Azure仮想マシンでEntity Frameworkデータベースを作成して接続するときにエラーが発生する

しかし、私はプロジェクトを実行しようとすると、このエラーを取得:どのようにこの問題を解決するために

Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

任意のアイデアを?

(N.B.プロジェクトは私のローカルマシン上で正常に動作している)

+0

何をしようとしていますか?リモートマシンやそれに何を配備しますか? – Mardoxx

+0

@Mardoxxローカルマシンではなく仮想マシンを開発するように設定する。 –

答えて

2

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.

あなたのSQL Serverインスタンスは、ローカルネットワーク上でホストされている場合、それは、リモートアクセス可能に構成されているかどうかを確認してください。リモートアクセスできない場合は、SQL Serverデータベースを仮想マシンまたはAzure SQLに移行する必要があります。次に、接続文字列を変更して、プロジェクトからアクセスできる新しいデータベースにアクセスすることができます。

I don't want to migrate anything, I want to make a new database on the VM..

あなたのVMで動作するローカルDBを使用できます。アプリケーションタイプがWebアプリケーションの場合は、データベースを次のように変更できます。 EFはApp_Dataフォルダに新しいデータベースを作成します。

それ以外の場合は、接続文字列にデータベースファイルの詳細パスを設定する必要があります。例えば、

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\Database1.mdf;Integrated Security=True 

私はちょうどインストールVS 2017でのAzureのWindows 10の仮想マシン上でLocalDBをテストし、私は最初のEFコードを使用する場合には、データベースを作成することができます。以下は私のテストコードです。

class Program 
{ 
    static void Main(string[] args) 
    { 
     using (SchoolContext context = new SchoolContext()) 
     { 
      context.Students.Add(new Student { ID = 1, FirstMidName = "FMN", LastName = "LN", EnrollmentDate = DateTime.Now }); 
      context.SaveChanges(); 
     } 

     Console.Write("Create database OK"); 
     Console.Read(); 
    } 
} 

public class Student 
{ 
    public int ID { get; set; } 
    public string LastName { get; set; } 
    public string FirstMidName { get; set; } 
    public DateTime EnrollmentDate { get; set; } 
} 

public class SchoolContext : DbContext 
{ 

    public SchoolContext() : base(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\amor\Desktop\TestEF\CodeFirstSample\Database1.mdf;Integrated Security=True") 
    { 
    } 

    public DbSet<Student> Students { get; set; } 
} 
+0

私は何も移行したくない、VM上で新しいデータベースを作成したい。 –

+0

あなたのコメント。 – Amor

+0

それだけです!出来た! Thnx Amor :) –

関連する問題