2015-11-30 25 views
0

我是新來的asp.net & mvc實體框架。 我做一個後臺管理應用在虛擬財產上添加一個條目

這裏是我的App類:

public class AsyApp 
{ 
    [Key] 
    public int AsyAppId { get; set; } 

    [StringLength(255)] 
    [Required] 
    [DisplayName("Nom")] 
    public string Name { get; set; } 

    [StringLength(255)] 
    [Required] 
    [RegularExpression(@"com\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+", ErrorMessage = "Le Bundle n'est pas de la bonne forme (ex: com.Company.AppName)")] 
    public string Bundle { get; set; } 

    public virtual Theme Theme { get; set; } 
    public virtual AppIdentity AppIdentity { get; set; } 

    public virtual ICollection<Scene> Scenes { get; set; } 

    public virtual ICollection<FilePath> FilePaths { get; set; } 
} 

應用程序有連接到它的AppIdentity:

public class AppIdentity 
{ 


    [DisplayName("Ecran d'accueil")] 
    public virtual FilePath Splashscreen { get; set; } 

    [DisplayName("Icon")] 
    public virtual FilePath Icon { get; set; } 

    [DisplayName("Logo Application")] 
    public virtual FilePath LogoApp { get; set; } 

    [DisplayName("Logo Client")] 
    public virtual FilePath LogoClient { get; set; } 

    [Key, ForeignKey("AsyApp")] 
    public int AsyAppId { get; set; } 

    public virtual AsyApp AsyApp { get; set; } 

} 

這裏是我的DbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection", throwIfV1Schema: false) 
    { 
    } 

    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 

    public DbSet<AsyApp> AsyApps { get; set; } 

    public DbSet<Theme> Themes { get; set; } 

    public DbSet<Scene> Scenes { get; set; } 

    public DbSet<AppIdentity> AppIdentities { get; set; } 

    public DbSet<FilePath> FilePaths { get; set; } 
} 

我有一個視圖,我編輯AppIdentity的應用程序陽離子。 我想更新例如Splashscreen屬性。

,我想到的是,當我做

appIdentity.Splashscreen = new FilePath(); 

它添加的文件路徑dbset條目的行爲; 將新文件路徑條目的AsyAppId設置爲當前的AsyApp。 但它不工作...

這裏是我的控制器

[HttpPost] 
    [ValidateAntiForgeryToken] 
     public ActionResult Edit(AppIdentity appIdentity) 
    { 

     var f = new FilePath(); 
     f.AsyAppId = appIdentity.AsyAppId; 
     db.FilePaths.Add(f); 
     db.SaveChanges(); 
     appIdentity.Splashscreen = f; 
     db.SaveChanges(); 
     return RedirectToAction("Edit", "AppIdentity", new { asyAppId = appIdentity.AsyAppId });}} 

任何幫助嗎?

預先感謝您,這會幫助我很多。 我只是不完全瞭解屬性如何虛擬作品的權利知道

+0

所做的第一件工作的SaveChanges?我的意思是,是否將實體添加到FilePaths集合中?你在你的數據庫中找到它嗎? – user449689

回答

0

嘗試把這個代碼appIdentity.Splashscreen = f;後:

 db.AppIdentities.Attach(appIdentity); 
     context.Entry(appIdentity).State = EntityState.Modified; 
     db.SaveChanges(); 
+0

嗨, 感謝您的回答。它炒鍋! 我不得不刪除第一個db.SaveChanges()雖然 你能告訴我什麼附件做? 謝謝! – Ziboo

+0

當您對實體對象進行更改時,會在db.SaveChanges()操作期間跟蹤並保存這些更改。但是對於要跟蹤的對象,它需要從數據庫中提取,或者像在你的例子中一樣明確附加。它只是說,從現在起,跟蹤這個對象的變化,並且當SaveChanges被調用時,如果有任何變化,將它保存到數據庫中。對於要連接的對象,必須分配Id。 – Aleksa

+0

好的,謝謝;) 其實它並不完整。當我添加一個新的FilePath並設置Splashscreen屬性時,它首次運行。 但是,當我不想更新它(如更改FileName屬性)它不起作用... – Ziboo