实体框架使用c#

时间:2016-02-08 14:31:40

标签: c# entity-framework-6

我有一个应用程序,连接字符串按用户和计算机更改。那说我做了一个测试连接字符串只是为了测试它。然后,一旦工作,我将添加动态连接字符串。

经过一番研究后,我发现连接字符串存在问题。

如果有人能告诉我我错在哪里,我会很感激。

var s = @"metadata=res://*/ProcurementModel.csdl|res://*/ProcurementModel.ssdl|
          res://*/ProcurementModel.msl;provider=System.Data.SqlClient;
          provider connection string=&';data source=JAMES-DESKTOP\SQLEXPRESS;initial catalog=MyDatabase;persist security info=True;user id=User;password=*****;MultipleActiveResultSets=True;App=EntityFramework&';";

更新

public ProcurementContext()
{
    var s =
            @"metadata=res://*/ProcurementModel.csdl|res://*/ProcurementModel.ssdl|
            res://*/ProcurementModel.msl;provider=System.Data.SqlClient;
            provider connection string=&';data source=JAMES-DESKTOP\SQLEXPRESS;initial catalog=MyDatabase;persist security info=True;user id=jd;password=P@ssw0rd;MultipleActiveResultSets=True;App=EntityFramework&';";

    _connectionString = s;
}

获取数据:

public List<Procurement> ParcelListByUser(string userName)
{
    using (var context = new ProcurementEntities(_connectionString))
    {
        return context.Procurements.Where(p => p.UserName == userName).ToList();
    }
}

构造

public ProcurementEntities(string connectionString)
    : base(connectionString)
{
}

错误讯息:

  

初始化字符串的格式不符合规范   从指数165开始。

配置:

采购的配置

public class ProcurementConfiguration:DbConfiguration
{
    public ProcurementConfiguration()
    {
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
        SetDefaultConnectionFactory(new SqlConnectionFactory(ConnectionStrings.LocalConnectionString));
    }
}

2 个答案:

答案 0 :(得分:2)

我认为问题出在您的连接字符串中。我做了同样的事情,我有一个以自动方式创建连接字符串的功能。

代码:

    public static string GetConnectionString()
    {
        // Build the provider connection string with configurable settings
        string cn = "server=" + mdlImpostazioni.p.dbServer;
        cn += ";database=" + mdlImpostazioni.p.dbName;
        cn += ";uid=" + mdlImpostazioni.p.dbUser;
        cn += ";pwd=" + mdlImpostazioni.p.dbPassword + ";";
        var providerSB = new SqlConnectionStringBuilder(cn);
        var efConnection = new EntityConnectionStringBuilder();
        // or the config file based connection without provider connection string
        efConnection.Provider = "System.Data.SqlClient";
        efConnection.ProviderConnectionString = providerSB.ConnectionString;
        // based on whether you choose to supply the app.config connection string to the constructor
        efConnection.Metadata = @"res://*";  //-----> very important
        return efConnection.ToString();
    }

通过这种方式,我将返回值传递给我的db上下文构造函数,就像在代码中一样。我的构造函数是:

public partial class Entities : DbContext
{
    public Entities(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {

    }

    public void Close()
    {
        this.Dispose();
    }
}

尝试并询问您是否有问题。

答案 1 :(得分:0)

动态更改连接字符串的一种方法是仅更改实体框架底层连接的连接字符串。像这样:

myDbContext.Database.Connection.ConnectionString = "Data source=JAMES-DESKTOP\SQLEXPRESS;initial catalog=MyDatabase;persist security info=True;user id=DynamicUser;password=DynamicPassword;MultipleActiveResultSets=True;App=EntityFrameworkForUser";

这样,您的应用程序就不必硬编码与实体框架相关的令牌(即元数据)。