Windows Phone 8 - exception while Submit Changes

557 views Asked by At

I have a problem with local database in Windows Phone 8 app.

It's my DatabaseManager and Models

 public class DatabaseManager : DataContext
{
    // Specify the connection string as a static, used in main page and app.xaml.
    public static string DBConnectionString = "Data Source=isostore:/LocalMainDatabase.sdf";

    // Pass the connection string to the base class.
    public DatabaseManager(string connectionString) : base(connectionString) { }

    // Specify a single table for the to-do items.
    //public Table<GroupDatabaseModel> GroupDbModel;
    public Table<GroupDatabaseModel> GroupDbModel;
}




[Table]
public class GroupDatabaseModel : INotifyPropertyChanged, INotifyPropertyChanging
{
    private int id;
    //private string name { get; set; }


    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int Id
    {
        get
        {
            return id;
        }
        set
        {
            if (id != value)
            {
                NotifyPropertyChanging("Id");
                id = value;
                NotifyPropertyChanged("Id");
            }
        }
    }


    /*[Column(IsPrimaryKey = false, IsDbGenerated = true, DbType = "NVarChar(30) NOT NULL", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (name != value)
            {
                NotifyPropertyChanging("Name");
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }*/


    public event PropertyChangingEventHandler PropertyChanging;
    public void NotifyPropertyChanging(string propertyName)
    {
        if (this.PropertyChanging != null)
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
    }



    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

}

In my ViewModel I create database

DatabaseManager db;
        using (db = new DatabaseManager("isostore:/LocalMainDatabase.sdf"))
        {
            if (db.DatabaseExists() == false)
            {
                // Create the database.
                db.CreateDatabase();



                GroupDatabaseModel k1 = new GroupDatabaseModel { Id = 11 }; 



                db.GroupDbModel.InsertOnSubmit(k1);


                try
                {
                    db.SubmitChanges();
                }
                catch (Exception exx)
                {
                    //  Console.WriteLine(exx);
                    // Make some adjustments. 
                    // ... 
                    // Try again.
                    db.SubmitChanges();
                }

                // Define query to gather all of the to-do items.
                var toDoItemsInDB = from GroupDatabaseModel todo in db.GroupDbModel select todo;

Unfortunately I get exception while submit changes :

"An exception of type 'System.NotSupportedException' occurred in System.Data.Linq.ni.dll but was not handled in user code

Additional information: Insertion of a row consisting only of database generated values is not supported in this data provider."

What is wrong ?

0

There are 0 answers