2016-07-18 198 views
2

我有3個表:如何在實體框架許多更新了許多表

新聞

public partial class News 
{ 
    public News() 
    { 
     this.Categories = new HashSet<Category>(); 
    } 

    public int NewsId { get; set; } 
    public string NewsTitle { get; set; } 
    public string NewsBody { get; set; } 
    public System.DateTime NewsDate { get; set; } 
    public string NewsImagePath { get; set; } 

    public virtual ICollection<Category> Categories { get; set; } 
} 

類別

public partial class Category 
{ 
    public Category() 
    { 
     this.News = new HashSet<News>(); 
    } 

    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 

    public virtual ICollection<News> News { get; set; } 
} 

和中間表是新聞分類是多對多的關係。

我通過向數據庫發送t-sql更新了中間表。

現在的問題是如何與LINQ而不是T-SQL更新中間表

所有我想要的是更換使用LINQ的代碼做同樣的功能

這裏是我的代碼:

if (model.SelectedCategoriesIds != null) 
{ 
    string SqlCommandToInsert = string.Empty; 
    string SqlCommandToDelete = string.Empty; 

    var OriginalCategoriesIds = NewsToUpdate.Categories.Select(c => c.CategoryId); 

    int[] selectedCategoriesIds = model.SelectedCategoriesIds.Split(',').Select(Int32.Parse).ToArray(); 

    foreach (var CategoryId in OriginalCategoriesIds) 
    { 
     if (!selectedCategoriesIds.Contains(CategoryId)) 
     { 
      SqlCommandToDelete += "Delete from NewsCategory where NewsId=" + NewsToUpdate.NewsId + " and CategoryId=" + CategoryId; 
     } 
    } 

    foreach (var SelectedId in selectedCategoriesIds) 
    { 
     if (!OriginalCategoriesIds.Contains(SelectedId)) 
     { 
      SqlCommandToInsert += "Insert into NewsCategory (NewsId,CategoryId) values(" + NewsToUpdate.NewsId + "," + SelectedId + ")"; 
     } 
    } 

    if (!string.IsNullOrEmpty(SqlCommandToDelete) || !string.IsNullOrEmpty(SqlCommandToInsert)) 
    { 
     db.Database.ExecuteSqlCommand(SqlCommandToDelete + SqlCommandToInsert); 
    } 
} 
else 
{ 
    db.Database.ExecuteSqlCommand("Delete from NewsCategory where NewsId=" + NewsToUpdate.NewsId); 

} 
+0

從DbContext獲取新聞對象。對「類別」列表進行任何更改,然後調用DbContext.SaveChanges(); – Mangist

+0

謝謝你的幫助...我試過它不起作用...你能用我的代碼告訴我怎麼做? – Lucia

+0

你可以顯示你的第三張桌子的屬性嗎?或者它只是兩個ID字段? – tCoe

回答

0

你必須直接使用集合新聞和類別。爲這些集合添加或刪除實體。之後,你必須保存新聞和類別實體。例如:

 var category = new Category(); 
     var news = new News(); 
     var removedNews = new News(); 
     var newCategory = new Category(); 

     category.News.Remove(removedNews); 
     news.Categories.Add(newCategory); 
     //to do save category and news 
1

您希望使用DbContext直接對您的實體模型進行更改,然後保存更改。 LINQ to Entity將負責處理SQL語句。下面的這個例子應該讓你找到正確的道路來實現這個目標。你不應該寫任何SQL語句。

// Replace with your real model from MVC 
var model = new Model { SelectedCategoriesIds = new List<int>() }; 
model.SelectedCategoriesIds.Add(1); 
model.SelectedCategoriesIds.Add(2); 
model.SelectedCategoriesIds.Add(3); 

var newsToUpdate = new News { Categories = new List<Category>() }; // Replace with your call to database 

// Use an Entity Framework context to update our db model 
using (var dbContext = new MyDbContext()) 
{ 
    // First clear existing categories 
    newsToUpdate.Categories.Clear(); 

    // Now add selected categories 
    foreach (var selectedCategory in model.SelectedCategoriesIds) 
    { 
     var dbCat = dbContext.Categories.Single(c => c.Id == selectedCategory); 
     newsToUpdate.Categories.Add(dbCat); 
    }; 

    // Save changes 
    dbContext.SaveChanges(); 
} 
相關問題