如何在EntityFrame中导航3个表工作WPF

时间:2016-12-01 08:32:02

标签: wpf entity-framework

我是WPF的新手。我有一个使用Entity框架工作从数据库生成的Country类。当我按pageup或pageDown键导航到Country类以显示Country id并命名下一个元素的第一个到最后一个元素。当我运行代码时,我得到了Exception,请告诉我如何导航3个表 我的数据库类:

公共部分国家{

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Country()
    {
        this.PurchaseOrderLineItems = new HashSet<PurchaseOrderLineItem>();
        this.States = new HashSet<State>();
    }

    public int CountryID { get; set; }
    public string Country_Name { get; set; }
    public string Country_Code { get; set; }
    public Nullable<System.DateTime> CreatedDate { get; set; }
    public Nullable<int> CreatedBy { get; set; }
    public Nullable<System.DateTime> LastUpdatedDate { get; set; }
    public Nullable<int> LastUpdatedBy { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<PurchaseOrderLineItem> PurchaseOrderLineItems { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<State> States { get; set; }
}

导航Businesslogic类是:

public Country NavigateCoutryCodebyID(int countryCodeID,NavigationType nav)         {

        //.Include("State").Include("PurchaseOrderLineItem")
        Country countryCode = null;
        try
        {
            using (LWLLCDBEntities dbEntities = new LWLLCDBEntities())
            {
                //current item
                switch (nav)
                {
                    case NavigationType.First:
                        countryCode = dbEntities.Countries.FirstOrDefault();
                        break;
                    case NavigationType.Last:
                        countryCode = dbEntities.Countries.OrderByDescending(t => t.CountryID).FirstOrDefault();
                        break;
                    case NavigationType.Current:
                        countryCode = dbEntities.Countries.Where(i => i.CountryID == countryCodeID).FirstOrDefault();
                        break;
                    case NavigationType.Next:
                        countryCode = dbEntities.Countries.Where(i => i.CountryID > countryCodeID).FirstOrDefault();

                        if (countryCode == null)
                            countryCode = dbEntities.Countries.FirstOrDefault();
                        break;
                    case NavigationType.Prev:
                        countryCode = dbEntities.Countries.Where(i => i.CountryID < countryCodeID).FirstOrDefault();

                        if (countryCode == null)
                            countryCode = dbEntities.Countries.OrderByDescending(t => t.CountryID).FirstOrDefault();
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            if (log.IsErrorEnabled)
            {
                log.Error("BusinessLogic.NavigateCountryCodebyID in Business Logic threw exception: ", ex);
            }
        }
        return countryCode;


    }
我会写的 case NavigationType.First: countryCode = dbEntities.Countries.Include(“State”)。Include(“PurchaseOrder LineItem”)。FirstOrDe fault(); 打破; case NavigationType.Last: countryCode = dbEntities.Countries.Include(“State”)。Include(“PurchaseOrder LineItem”)。OrderByDe scending(t =&gt; t.CountryID).FirstOrDefault(); 打破;  像这样也是同样的例外。当我加载它将显示247条记录。当我导航此异常时

0 个答案:

没有答案