开发者

Entity Framework is deleting an Entry

I'm fetching information from a webpage in two pages:

First page: - Content c1 is created and a Translation c1.t1 is created; - Content c2 is created and Translation c2.t1 is created;

Second page: - The system detects that c1 already exists and just adds c1.t2 to the proper table; - The system detects that c2 already exists and just adds c2.t2 to the proper table;

Somehow, on 开发者_StackOverflowthe second page, the system is overritting c1.t1 with c1.t2 and only the second translation is available on the database. When debbugging, found that it is deletting c1.t1 at some point but I couldn't figure out why.

This is my actual stuff:

  • EF 4.1
  • Code-First Aproach
  • DbContext

I have this POCO Entities (minimized):

RegionalContent: - It's like a tranlation and regional info about a content:

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; }
}

Content: - Unique content per 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; }
}

Region - Pretty straight forward:

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 class has nothing different, just DbSets.

One content has many translations. One translation has one content related. The translation primary key is compound of content guid and region id.

I have a class in Model that populates the database and creates a local list that the View uses to display information. That way, I only access the Database one time to save, and don't need to retrieve information when it is saved.

Here is only the important information about this class:

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);
        }
    }
}


you are missing a db.SaveChanges() after

db.SaveChanges();

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜