魏韋唯
魏韋唯

Reputation: 21

There is already an open DataReader associated with this Connection

I USE EF+MySql,the database contain two foriegnkey, and when i run my project,it say:There is already an open DataReader associated with this Connection which must be closed first, then i add MultipleActiveResultSets=true to connection string in web.config, i try again, it show me: The format of the initialization string does not meet specifications, how i can fix this problem? the code difinite foriegnkey and the wrong initialization code list as below:

#region EDM 关系源元数据

[assembly: EdmRelationshipAttribute("blogModel", "cid", "cls", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(blog.Models.cls), "news", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(blog.Models.news), true)]
[assembly: EdmRelationshipAttribute("blogModel", "uid", "users", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(blog.Models.users), "news", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(blog.Models.news), true)]

#endregion

#region 导航属性

    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("blogModel", "cid", "cls")]
    public cls cls
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<cls>("blogModel.cid", "cls").Value;
        }
        set
        {
            ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<cls>("blogModel.cid", "cls").Value = value;
        }
    }
    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    [BrowsableAttribute(false)]
    [DataMemberAttribute()]
    public EntityReference<cls> clsReference
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<cls>("blogModel.cid", "cls");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<cls>("blogModel.cid", "cls", value);
            }
        }
    }

    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("blogModel", "uid", "users")]
    public users users
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<users>("blogModel.uid", "users").Value;
        }
        set
        {
            ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<users>("blogModel.uid", "users").Value = value;
        }
    }
    /// <summary>
    /// 没有元数据文档可用。
    /// </summary>
    [BrowsableAttribute(false)]
    [DataMemberAttribute()]
    public EntityReference<users> usersReference
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<users>("blogModel.uid", "users");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<users>("blogModel.uid", "users", value);
            }
        }
    }

    #endregion


public partial class blogEntities : ObjectContext
{
    #region 构造函数

    /// <summary>
    /// 请使用应用程序配置文件的“blogEntities”部分中的连接字符串初始化新 blogEntities 对象。
    /// </summary>
    public blogEntities() : base("name=blogEntities", "blogEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// 初始化新的 blogEntities 对象。
    /// </summary>
    public blogEntities(string connectionString) : base(connectionString, "blogEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// 初始化新的 blogEntities 对象。
    /// </summary>
    public blogEntities(EntityConnection connection) : base(connection, "blogEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    #endregion

Upvotes: 1

Views: 2583

Answers (2)

raz
raz

Reputation: 31

Use different Open and close SQL connection names for each of the DataReaders. This will solve the issue.

As you are using same open and close database connection for multiple DataReader which is not supported by SQL server in VB.NET.

Upvotes: 3

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

As I know MultipleActiveResultSets are not available on MySql so you cannot add it to connection string (it is for MS SQL).

The reason for this problem is most probably lazy loading. You are iterating result of some query and in the same time you access not loaded navigation properties inside the loop. That requires additional query to be executed and that query requires a new data reader (the first one is still not closed because you are just iterating its result set).

The solution:

  • Either materiealize whole result set of the query you want to iterate by using ToList or perhaps also AsEnumerable
  • Or eager load navigation properties you want to use inside the loop by using Include

Upvotes: 3

Related Questions