在我工作的WPF应用程序中,我已经拥有了我的模型和sqlce数据库。 但是现在我使用普通的参数化查询来检索,更新或删除我的数据。
我仍然可以使用实体框架,还是为时已晚?我的模型实现了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);
}
}
THX!
答案 0 :(得分:3)
是的,你可以。
实体框架有三个工作流程:Database First
,Model First
和Code First
,因此您可以使用Database First