私の作業中の WPF アプリケーションには、既にモデルと sqlce データベースがあります。ただし、現在は通常のパラメーター化されたクエリを使用して、データを取得、更新、または削除しています。
これには Entity Framework を引き続き使用できますか?それとも遅すぎますか? 私のモデルは INotifyChangedProperty (MVVM アプリケーション) を実装しています。
現在の AddressModel の挿入メソッドの例。Entity Frameworkを使用してこれを変更したいと思います。
public int InsertNewAddress(bool isnew)
{
IsNew = isnew;
try
{
ArrayList paramList = new ArrayList();
paramList.Add(new SqlCeParameter() { ParameterName = "@Id", Value = Id, DbType = DbType.Guid, Size = 16, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Street", Value = Street, DbType = DbType.String, Size = 50, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Number", Value = Number, DbType = DbType.String, Size = 10, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Bus", Value = DBNull.Value, DbType = DbType.String, Size = 5, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@ZipCode", Value = Zipcode, DbType = DbType.String, Size = 10, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@City", Value = City, DbType = DbType.String, Size = 50, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Created", Value = DateTime.Now, DbType = DbType.DateTime, Size = 23, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Modified", Value = DateTime.Now, DbType = DbType.DateTime, Size = 23, Direction = ParameterDirection.Input });
paramList.Add(new SqlCeParameter() { ParameterName = "@Country", Value = Country, DbType = DbType.String, Size = 50, Direction = ParameterDirection.Input });
StringBuilder sb = new StringBuilder();
if (IsNew) // new address, insert
{
sb.Append("INSERT INTO [RF_Address] ");
sb.Append("([Id],[Street],[Number],[Bus],[ZipCode],[City] ,[DateCreated],[DateModified], [Country]) ");
sb.Append("VALUES ");
sb.Append("(@Id, @Street, @Number, @Bus, @ZipCode, @City, @Created, @Modified, @Country)");
}
else
{
sb.Append("UPDATE [RF_Address] ");
sb.Append("SET ");
sb.Append("[Street] = @Street, [Number] = @Number, [Bus] = @Bus, [ZipCode] = @ZipCode, [City] = @City, ");
sb.Append("[DateModified] = @Modified, [Country] = @Country ");
sb.Append("WHERE [Id] = @Id");
}
int result = SqlCeHelper.ExecuteNonQuery(sb.ToString(), paramList);
if (result != 1)
throw new CustomException("Insert address failed!");
return result;
}
catch (CustomException appEx)
{
throw new CustomException(appEx.Message, appEx);
}
catch (Exception ex)
{
throw new Exception("Error InsertNewAddress: " + ex.Message, ex);
}
}
どうも!