0

我从获取网页信息在两页:实体框架被删除条目

第一页: - 内容C1创建和翻译创建c1.t1; - 内容c2创建并翻译c2.t1创建;

第二页: - 如果系统检测到C1已经存在,只是增加了c1.t2到适当的表; - 系统检测到c2已经存在,只是将c2.t2添加到适当的表中;

不知何故,在第二页上,系统overritting c1.t1c1.t2,只有第二翻译可用的数据库。当debbugging时,发现它正在删除c1.t1在某些时候,但我不明白为什么。

这是我的真实的东西:

  • EF 4.1
  • 代码优先阿布罗奇
  • 的DbContext

我有这样的POCO实体(最小):

RegionalContent : - 这就像一个关于a的转换和区域信息内容:

public class XBLRegionalContent 
{ 
    [Key, Column(Order = 0)] 
    public string ContentId { get; set; } 

    [ForeignKey("ContentId")] 
    public virtual XBLContent Content { get; set; } 


    [Key, Column(Order = 1)] 
    public string RegionId { get; set; } 

    [ForeignKey("RegionId")] 
    public virtual XBLRegion Region { get; set; } 

    public string Name { get; set; } 
} 

内容: - 每个GUID独特的内容:

public class XBLContent 
{ 
    #region [ Properties ] 
    /// <summary> 
    /// The GUID 
    /// </summary> 
    [Key] 
    [StringLength(36, ErrorMessage="Must have 36 characters")] 
    [Required(ErrorMessage="Must have a unique GUID")] 
    public string GUID { get; set; } 

    public string Type { get; set; } 

    public virtual ICollection<XBLRegionalContent> RegionalInfo { get; set; } 
} 

地区 - 非常简洁明了:

public class XBLRegion 
{ 
    [Key] 
    [StringLength(5, ErrorMessage="ID must have 5 characters")] 
    [Required] 
    [RegularExpression(@"[a-z]{2}-[A-Z]{2}", ErrorMessage = "ID must be in ISO 639 standard")] 
    public string ID { get; set; } 

    public string Country { get; set; } 

    public string Language { get; set; } 
} 

的DbContext类有什么不同,只是DbSets。

一个内容有很多翻译。一个翻译有一个相关的内容。翻译主键是content guid和region id的复合词。

我在Model中有一个类来填充数据库并创建一个View用来显示信息的本地列表。这样,我只能访问一次数据库进行保存,并且在保存时不需要检索信息。

这里只有关于这个类的重要信息:

public class XBLChart : IDisposable 
{ 
    XBLContentContext db = new XBLContentContext(); 
    private string baseurl = "http://foo.bar/"; 

    public string Locale { get; private set; } 
    public string HTML { get; private set; } 
    public string URL { get; set; } 
    public ContentType Type { get; private set; } 

    public List<XBLContent> Contents { get; set; } 

    public XBLChart(ContentType type, string sort, string locale) 
    { 
     Type = type; 

     if (sort == null) 
      sort = Enum.GetName(typeof(SortBy), SortBy.OfferStartDate); 

     if (locale != null && locale.Length == 5) 
      Locale = locale; 
     else 
      Locale = "en-US"; 

     URL = baseurl + Locale + "/" + sort; 
     HTML = FeedUtils.RequestHTML(URL); 

     Contents = new List<XBLContent>(); 

     PopulateList(); 
    } 

    private void PopulateList() 
    { 
     MatchCollection itens = Regexes.ChartItems().Matches(HTML); 
     MatchCollection titulos = Regexes.ChartTitles().Matches(HTML); 

     int type = (int)Type; 
     int start = type * 12; 

     this.Title = HttpUtility.HtmlDecode(titulos[type].Groups["title"].Value); 

     if (titulos.Count < 8 && start > 1) 
     { 
      start = (type - 1) * 12; 
      type--; 
     } 

     XBLRegion region; 
     if (!db.XBLRegions.Any(x => x.ID == Locale)) 
     { 
      region = new XBLRegion { ID = Locale }; 
      db.XBLRegions.Add(region); 
      db.SaveChanges(); 
     } 
     else 
      region = db.XBLRegions.SingleOrDefault(x => x.ID == Locale); 


     for (int i = start; i < (start + 2); i++) 
     { 
      string guid = itens[i].Groups["guid"].Value; 

      XBLContent c = new XBLContent(guid); 
      if (!db.XBLContents.Any(x => x.GUID == guid)) 
      { 
       c.Type = Type.ToString(); 
       c.PopularInfo(Locale); 

       db.XBLContents.Add(c); 
      } 
      else 
       c = db.XBLContents.Single(x => x.GUID == c.GUID); 

      XBLRegionalContent regionalcontent = new XBLRegionalContent(guid, Locale);     
      if (!db.XBLRegionalInfos.Any(x => x.ContentId == guid && x.RegionId == Locale)) 
      { 
       if (c.HTML == null) 
        c.PopularInfo(Locale); 

       regionalcontent.Populate(c.HTML); 
       regionalcontent.Name = HttpUtility.HtmlDecode(itens[i].Groups["name"].Value); 

       db.XBLRegionalInfos.Add(regionalcontent);      
      } 
      else 
       regionalcontent = db.XBLRegionalInfos.Single(x => x.ContentId == guid && x.RegionId == Locale); 

      db.SaveChanges(); 

      c.RegionalInfo.Clear(); 
      regionalcontent.Region = region; 
      c.RegionalInfo.Add(regionalcontent); 

      Contents.Add(c); 
     } 
    } 
} 

回答

2

你缺少一个分贝。SaveChanges()后

db.SaveChanges(); 

c.RegionalInfo.Clear(); 
regionalcontent.Region = region; 
c.RegionalInfo.Add(regionalcontent);