System.NotSupportedException无法存储类型:Library.Models.UserModel

时间:2018-07-02 21:28:17

标签: c# sqlite dll uwp windows-10-universal

我正在用SQLite DB编写uwp程序。该程序使用通用的Windows库,该库包含控制DB的静态类。在异常Stack Trace中,我看到了sqlite的所有方法,即使它没有到达我在DB Manager类中放置的断点。

数据库所基于的模型:

public class UserModel
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Password { get; set; }
    public bool Connected { get; set; }
}

静态数据库管理器:

public static class UsersManager
{
    public static event EventHandler NewConnectionHandler=delegate { };

    private static string _path;
    private static SQLiteConnection _conn;

    static UsersManager()
    {
        _path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "usersdb.sqlite");
        _conn = new SQLiteConnection(new SQLitePlatformWinRT(), _path);
        _conn.CreateTable<UserModel>();
    }

    private static void OnNewConnection()
    {
        NewConnectionHandler.Invoke(null, EventArgs.Empty);
    }

    public static void Add(UserModel user)
    {
        _conn.Insert(user);
    }

    public static void Delete(int userId)
    {
         _conn.Delete<UserModel>(userId);

    }

    public static bool Contains(string name)
    {
        bool flag = false;
        var user = _conn.Find<UserModel>(u => u.Name == name);

        if (user != null)
            flag = true;

        return flag;
    }

    public static void ConnectUser(UserModel connectedUser,HubConnection hubConnection ,IHubProxy hubProxy)
    {
        _conn.Find<UserModel>(connectedUser).Connected = true;
        _conn.Update(connectedUser);
        var queryStringData = new Dictionary<string, string>();
        queryStringData.Add("username", connectedUser.Name);

        hubConnection = new HubConnection("http://127.0.0.1:8080", queryStringData);
        hubProxy = hubConnection.CreateHubProxy("ChatHub");
        if (hubConnection.State != ConnectionState.Connected)
        {
            hubConnection.Start();
        }

        OnNewConnection();

    }

    public static ObservableCollection<UserModel> ConnectedUsers
    {
        get
        {
            var allUsers = _conn.Table<UserModel>();
            ObservableCollection<UserModel> disconnectedUsers = new ObservableCollection<UserModel>();
            ObservableCollection<UserModel> connectedUsers = new ObservableCollection<UserModel>();
            foreach (var user in allUsers)
            {
                if (user.Connected)
                    connectedUsers.Add(user);
            }

            return connectedUsers;
        }

    }

    public static ObservableCollection<UserModel> DisconnectedUsers
    {
        get
        {
            var allUsers = _conn.Table<UserModel>(); 
            ObservableCollection<UserModel> disconnectedUsers = new ObservableCollection<UserModel>();
            foreach (var user in allUsers)
            {
                if (!user.Connected)
                    disconnectedUsers.Add(user);
            }

            return disconnectedUsers;
        }

    }
}

发生异常的地方:

private void SignIn(object sender, RoutedEventArgs e)
    {
        if (userViewModel.SignIn()) //here right after trying execute SignIn() 
        {
            Frame.Navigate(typeof(ContactsView), userViewModel);
        }
    }

UserViewModel中的SignIn方法:

public bool SignIn()
    {
        bool flag = false;
        if (Name == null || Password == null)
        {
            Error = "Name or password wasnt filled";
        }
        else if (UsersManager.Contains(Name) && !UserSettings.IsNameExict(Name))
        {
            Error = "Name already in use choose diffrent one";
        }
        else
        {
            UserModel user = new UserModel
            {
                Name = Name,
                Password = Password,
            };

            if (!UsersManager.Contains(Name))
            {
                UsersManager.Add(user);
                UserSettings.SaveUserName(Name);
            }

            UsersManager.ConnectUser(user,_hubConnection, _hubProxy);

            flag = true;
        }

        return flag;
    }

希望你们帮我,我真的想找到答案。谢谢

编辑1:

今天再次运行该程序,看来:该程序在此方法的UsersManager类中引发了异常:

public static void ConnectUser(UserModel connectedUser,HubConnection hubConnection ,IHubProxy hubProxy)
    {


        _conn.Find<UserModel>(connectedUser).Connected = true;//In this line!!!



        _conn.Update(connectedUser);
        var queryStringData = new Dictionary<string, string>();
        queryStringData.Add("username", connectedUser.Name);

        hubConnection = new HubConnection("http://127.0.0.1:8080", queryStringData);
        hubProxy = hubConnection.CreateHubProxy("ChatHub");
        if (hubConnection.State != ConnectionState.Connected)
        {
            hubConnection.Start();
        }

        OnNewConnection();

    }

1 个答案:

答案 0 :(得分:1)

问题是将UserModel实例发送到SQLiteConnection.Find<>方法。

find方法适用于表格的pk,因此

错误:

_conn.Find<UserModel>(connectedUser);

右:

_conn.Find<UserModel>(connectedUser.Id);

它还可以基于模型的任何其他属性获取lambda表达式:

_conn.Find<UserModel>(u=>u.Name == connectedUser.Name);