문제

I have problem with windows phone 8 local database. That's how my database code looks:

  [Table(Name = "Folders")]
    public class FolderTable : INotifyPropertyChanged, INotifyPropertyChanging, IEqualityComparer<FolderTable>
    {
        private string _folderName;
        private string _description;
        private string _password;
        private string _tileColorName;

        [Column(IsPrimaryKey = true)]
        public string FolderName
        {
            get { return _folderName; }
            set
            {
                if (_folderName != value)
                {
                    NotifyPropertyChanging();
                    _folderName = value;
                    NotifyPropertyChanged();
                }
            }
        }

        [Column(CanBeNull = false)]
        public string Description
        {
            get { return _description; }
            set
            {
                if (_description != value)
                {
                    NotifyPropertyChanging();
                    _description = value;
                    NotifyPropertyChanged();
                }
            }
        }

        [Column]
        public string Password
        {
            get { return _password; }
            set
            {
                if (_password != value)
                {
                    NotifyPropertyChanging();
                    _password = value;
                    NotifyPropertyChanged();
                }
            }
        }

        [Column]
        public string TileColorName
        {
            get { return _tileColorName; }
            set
            {
                if (_tileColorName != value)
                {
                    NotifyPropertyChanging();
                    _tileColorName = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private void NotifyPropertyChanging([CallerMemberName] string propertyName = "")
        {
            if ( PropertyChanging != null)
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if ( PropertyChanged != null )
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;


        public bool Equals(FolderTable x, FolderTable y)
        {
            return x.FolderName == y.FolderName;
        }

        public int GetHashCode(FolderTable obj)
        {
            return obj.FolderName.GetHashCode();
        }
    }

Folder add:

    public Task Add(Folder folder)
    {
        var folderTable = GetFolderTable(folder);

        _databaseContext.Folders.InsertOnSubmit(folderTable);

        return Task.FromResult(true);
    }

Commit:

public Task Commit()
{
    try
    {
        _databaseContext.Folders.Context.SubmitChanges();
    }
    catch (DuplicateKeyException duplicateKeyException)
    {
        _databaseContext.Dispose();
        _databaseContext = new FolderDbContext(FolderDbContext.DBConnectionString);

        throw new FolderAlreadyExistsException(duplicateKeyException);
    }

    return Task.FromResult(true);
}

When I Add new Folder (folder with such name didn't exist before), Commit and then I try add folder with same name DuplicateKeyException is thrown and handled correctly. However after context gets recreated in catch section the next call to Add and Commit folder with same name throws SqlCeException with message: "A duplicate value cannot be inserted into a unique index. [ Table name = Folders,Constraint name = PK_Folders ]". Is that normal behaviour or am I doing something wrong? In what cases DuplicateKeyException is thrown? How to catch SqlCeException? I can't even see System.Data.SqlServerCe namespace.

도움이 되었습니까?

해결책

use reflection to get the type name of the ex eption, if you wish to catch the SqlCeException explicitly:

ex.GetType().Name == "SqlCeException"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top