实体框架神秘的连接字符串错误

时间:2013-08-23 10:44:03

标签: c# entity-framework connection-string

我在Entity Framework 5中使用此连接字符串:

<connectionStrings>
<add name="RBSDbContext" connectionString="data source=.;MultipleActiveResultSets=true;initial catalog=RBS; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

以这种方式添加内容有效:

ctx.Categories.Add(new Category {Name="Another" });
ctx.SaveChanges();

以这种方式添加东西也有效:

 ctx.Set<Category>.Add(new Category {Name="Another" });
 ctx.SaveChanges();

以这种方式添加东西不起作用:

public T Add<T>(T item, bool commit = false) where T : class
{
        var aSet = this.Set<T>();

        aSet.Add(item);

        if (commit)
        {
            SaveChanges();
        }

    return item;
}

调用方法:

Repo.Add<Category>(new Category { Name = "Repo" }, true);

给出此错误:

[ArgumentException: Invalid value for key 'attachdbfilename'.]
System.Data.SqlClient.SqlConnectionString.VerifyLocalHostAndFixup(String& host, Boolean     enforceLocalHost, Boolean fixup) +907230
System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +4116
System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +24
System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +150
System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) +59
System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +4
System.Data.SqlClient.SqlConnection..ctor(String connectionString) +26
System.Data.Entity.Infrastructure.SqlConnectionFactory.CreateConnection(String nameOrConnectionString) +382
System.Data.Entity.Infrastructure.LocalDbConnectionFactory.CreateConnection(String nameOrConnectionString) +392
System.Data.Entity.Internal.LazyInternalConnection.Initialize() +581
System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName() +31
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +363
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +34
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +124
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +33
System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +226
System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) +228
System.Data.Entity.DbSet`1.Add(TEntity entity) +72

任何人都可以帮助或解释为什么它适用于一个而不是另一个?以及为什么抱怨连接字符串?

0 个答案:

没有答案