2013-01-17 30 views
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); 
    } 
} 

thx!

回答

3

是的,你可以。

實體框架有三個工作流程:Database FirstModel FirstCode First,所以三者之間可以使用Database First

+0

THX的快速回復。如果我理解正確,那會創建新模型嗎?我的應用程序是一個WPF MVVM應用程序,所以我的模型實現了INotifyPropertyChanged。我可以使用從實體框架生成的模型來做到這一點嗎? – PitAttack76

+0

是的,因爲您想使用'EF',您將會強制遷移您的代碼。 –

+0

我會在週末看它..只是爲了看看我是否理解你的答案是正確的,EF將創建新的模型,我可以使屬性INotifyPropertyChanged? – PitAttack76

相關問題