尝试在另一台计算机上访问或创建 Entity Framework code first 本地文件时出错

Getting an error when trying to access or create Entity Framework code first local file on a different computer

最近几天我一直在为这个问题苦苦挣扎,我尝试了各种连接字符串,但从未设法让它工作。 我有一个 C# 应用程序,我在其中使用 Entity Framework 创建本地数据库文件

public class MyDBContext : DbContext
{
    private readonly static MyDBContext _dbcontext = new MyDBContext();

    public static MyDBContext getDBContext()
    {
        return _dbcontext;
    }

    private MyDBContext () : base(@"Data Source=(LocalDB)\v11.0;Initial Catalog=mydb;Integrated Security=True") {
            //Database.Initialize(true); <-- I also tried commenting and leaving this
    }
}

所以这在 VS 中确实运行良好,它在用户文件夹中创建了 mydb 文件。 我还尝试将 AttachDbFilename=|DataDirectory|mydb.mdf; 替换为 Initial Catalog=mydb; 这也有效并读取应用程序旁边的文件。 但是一旦我将 exe(和必要的 DLL)复制到另一台 PC 或 VM,我在所有情况下都会在 constructor 处收到以下错误:

System.Data.SqlClient.SqlException (0x80131904): 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) at 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)

您可以安装 SQL express,LocalDB 是一个选项(IIRC 默认不选择),或者有一个独立的 LocalDB 安装程序 here