0

我将此连接字符串与 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

谁能帮助或解释为什么它适用于一个而不是另一个?为什么它抱怨连接字符串?

4

0 回答 0