实体框架正在删除条目

时间:2011-09-02 14:50:55

标签: c# entity-framework asp.net-mvc-3 entity-framework-4.1 ef-code-first

我从网页上获取两页信息:

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

第二页:   - 系统检测到 c1 已存在,只需将 c1.t2 添加到正确的表中;   - 系统检测到 c2 已存在,只需将 c2.t2 添加到正确的表格中;

不知何故,在第二页上,系统使用 c1.t2 重写 c1.t1 ,并且只有第二个转换可用于数据库。在进行调试时,发现它在某些时候正在解除 c1.t1 ,但我无法弄清楚原因。

这是我的实际内容:

  • EF 4.1
  • Code-First Aproach
  • 的DbContext

我有这个POCO实体(最小化):

RegionalContent: - 这就像是关于内容的翻译和区域信息:

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。

一个内容有很多翻译。一个翻译有一个内容相关。翻译主键是内容guid和区域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);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你错过了

之后的db.SaveChanges()
db.SaveChanges();

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