Windows Phone with SQL CE with ModelBase not updating entities

Topics: Issues / bugs, Questions
Sep 3, 2013 at 9:49 AM
If I derive from ModelBase in my Model and then try to update an entity, it won't work. As soon as I comment the ": ModelBase" out, it works.

The property "ServerUri" gets updated via
var setting = db.Settings.First();
setting.ServerUri = Setting.ServerUri;
but the changeSet shows no Update right before i SubmitChanges()
var changeSet = db.GetChangeSet();
And this only happens with ModelBase in the Setting Class.
I can't find my error :( Plz help!

MyModel
[Table]
public class Setting 
    //: ModelBase
{
    #region Version property

    [Column(IsVersion = true)] private Binary version;

    #endregion

    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public Guid Id { get; set; }

    [Column]
    public string ServerUri { get; set; }
}
MyViewModel
public class SettingsViewModel : ViewModelBase
{
    #region Fields

    #endregion

    #region Constructors

    #endregion

    #region Properties

    #region Setting model property

    /// <summary>
    /// Gets or sets the Setting value.
    /// </summary>
    [Model]
    [Expose("ServerUri")]
    public Setting Setting
    {
        get { return GetValue<Setting>(SettingProperty); }
        private set { SetValue(SettingProperty, value); }
    }

    /// <summary>
    /// Setting property data.
    /// </summary>
    public static readonly PropertyData SettingProperty = RegisterProperty("Setting", typeof (Setting));

    #endregion

    // TODO: Register models with the VmPropModel code template.
    // TODO: Register view model properties with the VmProp or VmPropViewModelToModel code template.

    #endregion

    #region Commands

    #region DeleteDatabase command

    private Command _deleteDatabaseCommand;

    /// <summary>
    ///     Gets the DeleteDatabase command.
    /// </summary>
    public Command DeleteDatabaseCommand
    {
        get { return _deleteDatabaseCommand ?? (_deleteDatabaseCommand = new Command(DeleteDatabase)); }
    }

    /// <summary>
    ///     Method to invoke when the DeleteDatabase command is executed.
    /// </summary>
    private async void DeleteDatabase()
    {
        using (var db = new MalmDataContext(App.DatabaseConnectionString))
        {
            var messageBoxAsync =
                await RadMessageBox.ShowAsync(AppResources.SettingsCommandDeletedatabaseMsgBoxDescription,
                    AppResources.SettingsCommandDeletedatabaseMsgBoxHeader,
                    MessageBoxButtons.YesNo);

            if (messageBoxAsync.Result == DialogResult.OK)
            {
                db.DeleteDatabase();
                db.CreateDatabase();

                Setting = new Setting();
            }
        }
    }

    #endregion

    #region SaveSettings command

    private Command _saveSettingsCommand;

    /// <summary>
    ///     Gets the SaveSettings command.
    /// </summary>
    public Command SaveSettingsCommand
    {
        get { return _saveSettingsCommand ?? (_saveSettingsCommand = new Command(SaveSettings)); }
    }

    /// <summary>
    ///     Method to invoke when the SaveSettings command is executed.
    /// </summary>
    private void SaveSettings()
    {
        using (var db = new MalmDataContext(App.DatabaseConnectionString))
        {
            if (!string.IsNullOrEmpty(Setting.ServerUri))
            {
                if (db.Settings.Any())
                {
                    var setting = db.Settings.First();
                    Debug.WriteLine(Setting.ServerUri);
                    setting.ServerUri = Setting.ServerUri;
                }
                else
                {
                    db.Settings.InsertOnSubmit(Setting);
                }

                var changeSet = db.GetChangeSet();
                db.SubmitChanges();
            }
        }
    }

    #endregion

    #endregion

    #region Methods

    protected override void Initialize()
    {
        using (var db = new MalmDataContext(App.DatabaseConnectionString))
        {
            Debug.WriteLine(db.Settings.Count());
            if (db.Settings.Any())
            {
                Setting = db.Settings.First();
            }
            else
            {
                Setting = new Setting();
            }
        }
        base.Initialize();
    }

    #endregion
}
Sep 3, 2013 at 9:49 AM
inserts are working by the way
Sep 4, 2013 at 7:56 AM
We need an exception. Can you enable "break on all exceptions" and see what happens?
Sep 4, 2013 at 1:05 PM
my IEqualityComparer<T> for the Models aren't working eighter :(
The ModelBase is activated on the AssetType class.

System.Data.SqlServerCe.SqlCeException occurred
HResult=-2147467259
Message=A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_AssetType_StockKeepingUnits ]
Source=SQL Server Compact ADO.NET Data Provider
ErrorCode=-2147467259
NativeError=25026
StackTrace:
   at System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr)
   at System.Data.SqlServerCe.SqlCeResultSet.InternalInsert(Boolean fMoveTo, Object sender, SqlCeUpdatableRecord record)
   at System.Data.SqlServerCe.SqlCeResultSet.Insert(SqlCeUpdatableRecord record, DbInsertOptions options)
   at System.Data.Linq.ChangeDirector.StandardChangeDirector.DoResultSetInsert(TrackedObject item)
   at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges()
   at C4IT.MALM.Client.WindowsPhone.ViewModels.MainViewModel.PersistSychroniziedData()
InnerException:
Sep 4, 2013 at 8:40 PM
How did you implement your equality comparers? You can override the Equals method to make sure that you use your own method.
Sep 23, 2013 at 5:48 AM
I did that... Without using the modelbase and viewmodelbase, it's working just fine :(
Sep 23, 2013 at 6:58 PM
Without a repro there is just too much going on so I can only speculate. Is it possible to create a small repro?