2010-01-29 56 views
5

我有一些MVC代碼將EF 3.5對象序列化爲匿名類型,作爲JSON結果返回給我的頁面上的AJAX調用。我遇到的障礙是,當我通過JSON將對象發送回服務器(並且讓ModelBinder將其反序列化爲我的EF類型)時,我必須在我的實體框架上下文中手動更新它,手動。或者至少這就是我現在正在做的。它沒有EntityKey,因此連接失敗。我最終不得不查找舊對象並按屬性更新它的屬性。任何想法呢?解決方案是使用我的對象傳遞EntityKey嗎?通過JSON傳輸實體框架對象的最佳方式

這是我有:

public void Update(Album album) 
    { 
     using (var db = new BandSitesMasterEntities()) 
     { 
      var albumToUpdate = db.Album.First(x => x.ID == album.ID); 

      albumToUpdate.AlbumTitle = album.AlbumTitle; 
      albumToUpdate.Description = album.Description; 
      albumToUpdate.ReleaseYear = album.ReleaseYear; 
      albumToUpdate.ImageURL = album.ImageURL; 
      albumToUpdate.OtherURL = album.OtherURL; 

      db.SaveChanges(); 
     } 
    } 

這裏就是我想要做的或類似的東西是什麼

public void Update(Album album) 
    { 
     using (var db = new BandSitesMasterEntities()) 
     { 
      db.Attach(album) 
      db.SaveChanges(); 
     } 
    } 
+0

您使用EF 4從更新的功能調用它呢? – ashraf 2010-01-30 00:50:18

+0

你爲什麼不使用數據服務? – Nix 2010-04-02 20:55:55

+0

@ashraf,他說他使用EF 3.5 – StriplingWarrior 2010-07-07 20:23:08

回答

0

你嘗試類似:

object original; 
var key = contexte..CreateEntityKey("EntitySet", modified); 
if(contexte.TryGetObjectByKey(key, out original)) 
{ 
    var originalEntity = (YourEntityType)original; 
    // You have to mannualy set your entityKey 
    originalEntity.YourEntityReference.EntityKey = new EntityKey("Entities.EntitySet", "Id", modified.YourEntity.Id); 

    contexte.ApplyPropertyChanges("EntitySet", modified); 
} 
contexte.SaveChanges(); 

假設你的EntityReference通過下拉菜單設置,你仍然有標識

1

,或者您可以使用AutoMapper映射那些字段給你,所以你只需在你的例子中添加一行。

+0

2行實際上 - 檢索關鍵字,然後映射。 – 2010-05-21 17:48:57

1

爲什麼不直接使用UpdateModel或TryUpdateModel控制器方法呢?它與EF的效果非常好,你甚至可以明確地設置包含的屬性列表。

id參數將通過MVC框架自動映射到指定id的窗體上的隱藏字段。

public void Update(int id, FormCollection collection) 
{ 
    using (var db = new BandSitesMasterEntities()) 
    { 
     var albumToUpdate = db.Album.First(x => x.ID == id); 

     //use UpdateModel to update object, or even TryUpdateModel 
     UpdateModel(albumToUpdate, new string[] { "AlbumTitle", "Description", "ReleaseYear", "ImageURL", "OtherURL" }); 

     db.SaveChanges(); 
    } 
} 
1

這對我們來說在EF 4.0中變得容易得多。這是我們在EF 3.5所做的:

public static void AttachAsModified(this ObjectContext objectContext, string setName, object entity, 
            IEnumerable<String> modifiedFields) 
{ 
    objectContext.AttachTo(setName, entity); 
    ObjectStateEntry stateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(entity); 
    foreach (String field in modifiedFields) 
    { 
     stateEntry.SetModifiedProperty(field); 
    } 
} 

然後:

using (var db = new BandSitesMasterEntities()) 
{ 
    db.AttachAsModified("Album", album, new string[] { "AlbumTitle", "Description", "ReleaseYear", "ImageURL", "OtherURL" }) 
    db.SaveChanges(); 
} 

,如果你有外鍵約束,它變得更加複雜,但它看起來像你不知道。

0

在你的相冊,實體的部分類,你可以定義的copyfrom功能和

partial class Album 
{ 
    public void CopyFrom(Album album) 
    { 
    //individual field copying here 
    } 
} 


public void Update(Album album) 
    { 
     ... 
     albumToUpdate.CopyFrom(album); 
     ... 
    }