【问题标题】:Error when trying to loop through entities from an Azure Table尝试循环访问 Azure 表中的实体时出错
【发布时间】:2012-03-30 13:18:42
【问题描述】:

我不断收到此错误,“当前值 'String.Empty' 类型与预期的 'System.Boolean' 类型不兼容”,当我尝试遍历 Azure 表中的一堆实体时,我是只是使用 Azure 的新手,所以这可能很容易,我得到的错误。

我的代码:

private void registerButton_Click(object sender, RoutedEventArgs e)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

        // Create the table client
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        // Get the data service context
        TableServiceContext serviceContext = tableClient.GetDataServiceContext();

        // Create a new customer entity
        user = new UserDetailsEntity();

        //Setting the fields of the new userEntity
        user.username = usernameText.Text;
        user.password = passwordText.Text;
        user.subscriptionID = subText.Text;
        user.subscriptionName = subscriptionNameText.Text;
        user.thumbprint = thumbprintText.Text;
        user.email = emailText.Text;
        user.phoneNumber = "3530" + numberText.Text;


        int rowCount = 1;


        CloudTableQuery<UserDetailsEntity> Query = (from en in serviceContext.CreateQuery<UserDetailsEntity>("userdetails")
                                                             select   en).AsTableServiceQuery<UserDetailsEntity>();

        //error occurs in the next line
        foreach (UserDetailsEntity ent in Query)
        {

            rowCount++;
        }

        user.RowKey = rowCount.ToString();





            // Add the new customer to the people table
            serviceContext.AddObject("userdetails", user);

            // Submit the operation to the table service
            serviceContext.SaveChangesWithRetries();
            //Set the variables so they can be retrieved when the next screen loads
            Application.Current.Properties["username"] = usernameText.Text;
            Application.Current.Properties["password"] = passwordText.Text;

            Window1 userHome = new Window1();
            this.Close(); //to close Password window 
            userHome.Show(); //to show Main form 
        }

【问题讨论】:

    标签: c# azure azure-storage azure-table-storage


    【解决方案1】:

    如果没有更多代码,我无法准确地告诉您问题出在哪里,但是这个异常是可以解释的。您正在尝试将布尔属性设置为字符串的值。

    如果您在代码注释中指出的错误发生在您的 foreach 中,那么我将检查您的 UserDetailsEntity 对象是如何设置的。可能有一个属性设置为布尔值,但您的数据以 String.Empty 形式返回。你在你的 foreach 中得到这个的原因是你的 LINQ 查询是 IQueryable 类型的,所以它不会真正执行和填充你的对象,直到你真正访问数据(通过你的 foreach)*。因此,您可以在您的 UserDetailsEntity 属性中放置断点,以查看它是哪一个,如果这不是从代码中看出来的。

    *请记住,这是 N+1 问题,您在循环的每次迭代中都调用数据库。您可以通过调用 .ToList() 将所有数据一次性加载到查询中来解决此问题...如果这对您来说是个问题,那就是。

    【讨论】:

    • 为回复干杯,错误发生在 foreach 行(查询中的 UserDetailsEntity ent)这就是为什么我对为什么会发生错误感到困惑
    • @StevenR 对不起,我错过了,我已经更新了我的答案以帮助您更好地解决这个问题
    • 非常感谢,我一定会按照您的建议去做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2012-07-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    相关资源
    最近更新 更多