2

在我工作的 WPF 应用程序中,我已经有了我的模型和 sqlce 数据库。但是现在我使用普通的参数化查询来检索、更新或删除我的数据。

我仍然可以为此使用实体框架还是为时已晚?我的模型实现了 INotifyChangedProperty(MVVM 应用程序)。

现在我的 AddressModel 中的插入方法示例。我想使用实体框架来改变它。

    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);
  }
}

谢谢!

4

1 回答 1

3

是的你可以。

Entity Framework 有三个工作流:和Database First,所以在这三个中你可以使用Model FirstCode FirstDatabase First

于 2013-01-17T08:10:21.897 回答