C# 实体框架正在删除一个条目

C# 实体框架正在删除一个条目,c#,entity-framework,asp.net-mvc-3,entity-framework-4.1,ef-code-first,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.t1与c1.t2的匹配度过高,数据库中只有第二个译文可用。当去bug时,我发现它在某个点删除了c1.t1,但我不知道为什么 这是我真正的东西: 环境足迹4.1 代码优先 DbContext 我有这个POCO实体(

我从两个页面中的网页获取信息:

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

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

不知何故,在第二页上,系统对c1.t1c1.t2的匹配度过高,数据库中只有第二个译文可用。当去bug时,我发现它在某个点删除了c1.t1,但我不知道为什么

这是我真正的东西:

  • 环境足迹4.1
  • 代码优先
  • DbContext
我有这个POCO实体(最小化):

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

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; }
}
DbContext类没有什么不同,只有dbset

一个内容有很多翻译。一个翻译与一个内容相关。翻译主键是内容guid和区域id的组合

我在模型中有一个类,它填充数据库并创建视图用于显示信息的本地列表。这样,我只需要访问数据库一次就可以保存,并且在保存时不需要检索信息

以下仅是有关该课程的重要信息:

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);
        }
    }
}
公共类XBLChart:IDisposable
{
XBLContentContext db=新的XBLContentContext();
专用字符串baseurl=”http://foo.bar/";
公共字符串区域设置{get;private set;}
公共字符串HTML{get;private set;}
公共字符串URL{get;set;}
公共ContentType类型{get;private set;}
公共列表内容{get;set;}
公共XBLChart(内容类型、字符串排序、字符串区域设置)
{
类型=类型;
if(sort==null)
sort=Enum.GetName(typeof(SortBy),SortBy.OfferStartDate);
if(locale!=null&&locale.Length==5)
语言环境=语言环境;
其他的
Locale=“en-US”;
URL=baseurl+Locale+“/”+排序;
HTML=FeedUtils.RequestHTML(URL);
Contents=新列表();
大众主义者();
}
私有void PopulateList()
{
MatchCollection itens=Regexes.ChartItems().Matches(HTML);
MatchCollection titulos=Regexes.ChartTitles().Matches(HTML);
int类型=(int)类型;
int start=类型*12;
this.Title=HttpUtility.HtmlDecode(titulos[type].Groups[“Title”].Value);
如果(titulos.Count<8&&start>1)
{
开始=(类型-1)*12;
类型--;
}
XBLRegion区域;
if(!db.XBLRegions.Any(x=>x.ID==Locale))
{
region=新XBLRegion{ID=Locale};
db.XBLRegions.Add(区域);
db.SaveChanges();
}
其他的
region=db.XBLRegions.SingleOrDefault(x=>x.ID==Locale);
对于(int i=开始;i<(开始+2);i++)
{
字符串guid=itens[i]。组[“guid”]。值;
XBLContent c=新的XBLContent(guid);
如果(!db.XBLContents.Any(x=>x.GUID==GUID))
{
c、 Type=Type.ToString();
c、 人口信息(地区);
db.XBLContents.Add(c);
}
其他的
c=db.XBLContents.Single(x=>x.GUID==c.GUID);
XBLRegionalContent regionalcontent=新的XBLRegionalContent(guid、区域设置);
如果(!db.XBLRegionalInfos.Any(x=>x.ContentId==guid&&x.RegionId==Locale))
{
如果(c.HTML==null)
c、 人口信息(地区);
regionalcontent.Populate(c.HTML);
regionalcontent.Name=HttpUtility.HtmlDecode(itens[i].Groups[“Name”].Value);
db.XBLRegionalInfos.Add(regionalcontent);
}
其他的
regionalcontent=db.XBLRegionalInfos.Single(x=>x.ContentId==guid&&x.RegionId==Locale);
db.SaveChanges();
c、 RegionalInfo.Clear();
区域内容。区域=区域;
c、 RegionalInfo.Add(regionalcontent);
内容。添加(c);
}
}
}

之后缺少db.SaveChanges()

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);
        }
    }
}
db.SaveChanges();

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