我试图在不使用App.Config的情况下连接到数据库,但我一直收到以下错误:
EntityFramework.dll中出现未处理的“System.Data.Entity.Core.EntityException”类型异常
其他信息:基础提供程序在ConnectionString上失败。
我无法看到我出错的地方,所以我想我会在这里问。
Low
提前感谢您的帮助。
答案 0 :(得分:0)
试试这个:make this parameterise
public Entities(string connString)
: base(connString)
{
}
并在创建Context类的对象时传递字符串连接字符串。
public class TestController : Controller
{
Entity _context = new Entity("data source=Dev-4;initial catalog=test1;
integrated security=True;MultipleActiveResultSets=True;
App=EntityFramework");
}
答案 1 :(得分:0)
试试这个:在这里你需要一次又一次地传递连接字符串 - >
public Model1()
: base(connString)
{
}
public static string connString = "data source=tesst;initial catalog=test1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
使用实体框架的数据库优先模型时使用此方法:
public test1Entities()
: base(nameOrConnectionString: ConnectionString())
{
}
private static string ConnectionString()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = "DEV-4";
sqlBuilder.InitialCatalog = "test1";
sqlBuilder.PersistSecurityInfo = true;
sqlBuilder.IntegratedSecurity = true;
sqlBuilder.MultipleActiveResultSets = true;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = "res://*/";
entityBuilder.Provider = "System.Data.SqlClient";
return entityBuilder.ToString();
}
答案 2 :(得分:0)
问题是您直接在连接字符串上传递sdf
文件。尝试更改:
ProviderConnectionString = @"C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"
要:
ProviderConnectionString = @"Data Source=C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"
或者更好的是,使用SqlCeConnectionStringBuilder
来构造此属性:
var connectionStringBuilder = new SqlCeConnectionStringBuilder();
connectionStringBuilder.DataSource = @"C:\RestOfPath\database.sdf";
connectionStringBuilder.Password = "3476dfg423434563466e85rcsd";
EFConnectionBuilder.ProviderConnectionString = connectionStringBuilder.ToString(),