EF Code First在开发时添加新列

时间:2017-11-29 15:48:46

标签: c# sql-server entity-framework azure

我仍在开发环境和开发服务器上开发数据库。

我没有权限在SQL Azure数据库上删除数据库,因此我无法使用ef database drop命令删除并重新创建数据库,因为我收到错误:

  

您确定要在服务器上删除数据库'xxx-Dev'吗?   'TCP:xxx1dev.database.windows.net,1433'? (是/否)y删除数据库   'XXX-DEV'。 System.Data.SqlClient.SqlException:服务器主体   “xxxDebug”无法访问数据库“master”下的   当前的安全背景。无法打开数据库“master”请求   登录。登录失败。

如果我使用迁移并添加新列并且我已经在内部有虚拟数据,那么新列可以为空?

如何处理这个问题,以便在数据已存在的情况下,新列不可为空?

我应该删除所有数据并将表重新设置为0,然后运行update命令以使用新列更新表吗?

1 个答案:

答案 0 :(得分:0)

我结束了删除我可以访问的表格,并使用ef数据库更新从头开始更新新列:

class UsersViewController: UITableViewController, UIGestureRecognizerDelegate {

private func gestureRecognizer(gestureRecognizer: UIPanGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UILongPressGestureRecognizer) -> Bool {return true}


    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return gestureRecognizer === longPressRecognizer &&
            (otherGestureRecognizer.view?.isDescendant(of:tableView) ?? false)
    }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "friendText", for: indexPath) as! friendTextCell

        print("keyArrrrray is: \(keyArray)")

        if indexPath.section == 0 && indexPath.row < keyArray.count {
            self.removeInstructions()
            cell.labelText.font = cell.labelText.font.withSize(17)
            let text = "> "+namesArray[indexPath.row] + ": " + linkArray[indexPath.row]
            let name = namesArray[indexPath.row]
            let link = linkArray[indexPath.row]
            let imageLink = imageURLArray[indexPath.row]
            let nameChCount = name.characters.count
            let linkChCount = link.characters.count

            let attributedString = NSMutableAttributedString(string: name + ": " + link, attributes: nil)

            let totalChCount = attributedString.string.characters.count

            let linkRange = NSMakeRange(0, nameChCount) // for the word "link" in the string above

            let linkAttributes: [String : AnyObject] = [
                NSForegroundColorAttributeName : UIColor.white, NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue as AnyObject]
            attributedString.setAttributes(linkAttributes, range:linkRange)

            cell.labelText.attributedText = attributedString

            cell.labelText.onCharacterTapped = { label, characterIndex, state in

                let highlight: [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.black, NSBackgroundColorAttributeName : UIColor.white]

                if state == true {
                    if characterIndex < nameChCount {
                        print("name press began at character \(characterIndex)")
                        attributedString.addAttributes(highlight, range:NSMakeRange(0, nameChCount))
                        cell.labelText.attributedText = attributedString
                    } else if characterIndex > nameChCount {
                        print("link press began at character \(characterIndex)")
                        let startPos = nameChCount + 2
                        let endPos = totalChCount-nameChCount-2
                        attributedString.addAttributes(highlight, range:NSMakeRange(startPos, endPos))
                        cell.labelText.attributedText = attributedString
                    }

                } else if state == false {

                    if characterIndex < name.characters.count {

                        if let userVC:UserViewTableViewController = self.storyboard?.instantiateViewController(withIdentifier: "UserVC") as? UserViewTableViewController {
                            userVC.userName = name
                            userVC.shareLink = link
                            userVC.imageLink = imageLink
                            self.navigationController?.pushViewController(userVC, animated: true)
                        }

                }

                if characterIndex > name.characters.count && characterIndex <= link.characters.count + name.characters.count {

                    //extract link from array
                    let link = self.linkArray[indexPath.row]
                    print("on click link is: \(link)")

                    //Present SafariViewController with link
                    let svc = SFSafariViewController(url: NSURL(string: link)! as URL)
                    self.present(svc, animated: true, completion: nil)

                }
                }

            }

        } else if keyArray.isEmpty && indexPath.section == 0 {

            cell.labelText.text = "..."

        }

        if indexPath.section == 1 && keyArray.count <= 1 {
            let message = "> Press the + button to add more friends."
            cell.labelText.animate(newText: message, characterDelay: TimeInterval(0.01 + Float(arc4random()) /  Float(UInt32.max)) / 200)
        } else if indexPath.section == 1 {
            cell.labelText.text = ""
        }

        return cell

    }