【问题标题】:Ria Services and navigation property issuesRia 服务和导航属性问题
【发布时间】:2011-07-21 08:47:33
【问题描述】:

我在使用 Silverlight4、Ria 服务和实体框架时遇到问题。

从我的 sl 客户端,我尝试通过 ria 服务获取一些数据,在我的 domainService 类中,此方法被调用:

public IQueryable<LastMinuteWachtLijstPromotie> GetLastMinuteWachtLijstPromoties(){
  IQueryable<LastMinuteWachtLijstPromotie> list = (IQueryable<LastMinuteWachtLijstPromotie>)this.ObjectContext.LastMinuteWachtLijstPromoties.Include("Promotie");

  return (from LastMinuteWachtLijstPromotie lwmp in list where lwmp.Actief select lwmp);
}

当我在调试模式下检查列表的内容时,它充满了 LastMinuteWachtLijstPromotie 类型的对象。 这些对象具有指向名为 Promotie 的对象的导航属性。 我可以访问这些 Promotie 对象的属性。

在silveright客户端上,加载完成时会调用一个方法:

public void OnLoadEntitiesCompleted(ServiceLoadResult<T> result) {

}

在此方法中,我按预期获得了所有请求的 LastMinuteWachtLijstPromotie 对象,属性 然而,促销是空的。

我在自动生成的元数据类中的属性 Promotie 上设置了 [Include] 标签 我使用 .Include("Promotie")

这些相同的方法用于我的领域模型中的不同对象,这非常有效。 此外,我似乎找不到 .edmx 文件与数据库映射和导航属性的差异。

有没有人遇到过同样的问题或知道解决方法?

元数据类:

[MetadataTypeAttribute(typeof(LastMinuteWachtLijstPromotie.LastMinuteWachtLijstPromotieMetadata))]
        public partial class LastMinuteWachtLijstPromotie
        {

            // This class allows you to attach custom attributes to properties
            // of the LastMinuteWachtLijstPromotie class.
            //
            // For example, the following marks the Xyz property as a
            // required property and specifies the format for valid values:
            //    [Required]
            //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
            //    [StringLength(32)]
            //    public string Xyz { get; set; }
            internal sealed class LastMinuteWachtLijstPromotieMetadata
            {

                // Metadata classes are not meant to be instantiated.
                private LastMinuteWachtLijstPromotieMetadata()
                {
                }

                public int AlertId { get; set; }

                public string ArtikelNummer { get; set; }

                public Nullable<int> ArtikelVariant { get; set; }

                public int LastMinuteWachtLijstPromotieId { get; set; }

                [Include]
                public Promotie Promotie { get; set; }

                public int PromotieArtikelId { get; set; }

                public int PromotieId { get; set; }

                public bool Actief { get; set; }

                public DateTime Aanmaakdatum { get; set; }
            }
        }


        [MetadataTypeAttribute(typeof(Promotie.PromotieMetadata))]
    public partial class Promotie
    {

        // This class allows you to attach custom attributes to properties
        // of the Promotie class.
        //
        // For example, the following marks the Xyz property as a
        // required property and specifies the format for valid values:
        //    [Required]
        //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
        //    [StringLength(32)]
        //    public string Xyz { get; set; }
        internal sealed class PromotieMetadata
        {

            // Metadata classes are not meant to be instantiated.
            private PromotieMetadata()
            {
            }

            public string ActieType { get; set; }

            public string AssortimentsManagerNaam { get; set; }

            public string AssortimentsManagerTeamIds { get; set; }

            [Display(Name = "Commerciele tekst")]
            [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Nokavision.ReclameFolder.UI.Web.Resources.ValidationResources))]            
            public string CommercieleTekst { get; set; }

            [Display(Name = " ")]
            public string CommercieleTekstDetails { get; set; }

            [Include]
            public Frame Frame { get; set; }

            public Nullable<int> FrameId { get; set; }

            public Nullable<DateTime> LastMinuteWijzigingsDatum { get; set; }

            public string Opmerkingen { get; set; }

            [Display(Name = "Op wachtlijst")]
            public Nullable<bool> OpWachtLijst { get; set; }

            //public Nullable<int> PromotieCopyId { get; set; }

            public int PromotieId { get; set; }

            [Include]
            public EntityCollection<PromotieLeverancier> PromotieLeveranciers { get; set; }

            [Include]
            public EntityCollection<PromotieMutatie> PromotieMutaties{ get; set; }

            //public Nullable<int> PromotieOrigineleId { get; set; }

            [Include]
            public EntityCollection<PromotieSymbool> PromotieSymbolen { get; set; }

            public string Status { get; set; }

            [Display(Name = "Promotie inhoud")]
            public string PromotieInhoud { get; set; }

            [Display(Name = "Promotie eenheid")]
            public string PromotieEenheid { get; set; }

            [Display(Name = "Promotie prijs")]
            public decimal PromotiePrijs { get; set; }
        }
    }

【问题讨论】:

  • 能否添加元数据类,尤其是 LastMinuteWachtLijstPromotie 和 Promotie?
  • 在原帖中添加了它们,tnx
  • 你定义了 NavigationProperty 没有添加外键?

标签: c# silverlight entity-framework wcf-ria-services


【解决方案1】:

Composition 属性添加到 LastMinuteWachtLijstPromotieMetadata 类的 Promotie 属性。那么它应该可以工作了。

public partial class LastMinuteWachtLijstPromotie {
  internal sealed class LastMinuteWachtLijstPromotieMetadata{
    [Include]
    [Composition]
    public Promotie Promotie { get; set; }
  }
}

【讨论】:

  • 嗯,我已经添加了这个,重建然后测试它,它似乎没有任何区别
【解决方案2】:

我知道这是一个较旧的帖子,很可能已经在其他地方得到了回答,但我只是偶然发现了它,因为没有人提供链接或更好的答案。

我目前正在使用 Silverlight 5,这对我有用(我认为该过程在 SL4 IIRC 中是相同的)。

当向客户端传播导航属性时,您需要使用 [Key] 和 [Association] 属性告诉 RIA 服务在某处存在关系,这与实体框架不同,它只是描述了如何将关系映射到适当的对象。

首先是元数据类:

[MetadataTypeAttribute(typeof(Category.CategoryMetadata))]
public partial class Category
{
    internal sealed class CategoryMetadata
    {
        private CategoryMetadata() {
        }

        [Key]
        public int Id { get; set; }

        public string NAME { get; set; }

        [Association("CategoryToProducts", "Id", "CAT")]
        [Include]
        public EntityCollection<Product> Products { get; set; }
    }
}

[MetadataTypeAttribute(typeof(Order.OrderMetadata))]
public partial class Order
{

    internal sealed class OrderMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private OrderMetadata() {
        }

        [Key]
        public int Id { get; set; }

        public int PRODID { get; set; }

        public DateTime DATE { get; set; }

        public bool DONE { get; set; }

        public int QTY { get; set; }

        [Association("OrderToProduct", "PRODID", "Id", IsForeignKey = true)]
        [Include]
        public Product Product { get; set; }
    }
}

[MetadataTypeAttribute(typeof(Product.ProductMetadata))]
public partial class Product
{
    internal sealed class ProductMetadata
    {
        private ProductMetadata() {
        }

        [Key]
        public int Id { get; set; }

        public int CAT { get; set; }

        public string NAME { get; set; }

        public string DESC { get; set; }

        public decimal PRICE { get; set; }

        public int QTY { get; set; }

        public long UPC { get; set; }

        [Association("ProdToCat", "CAT", "Id", IsForeignKey = true)]
        [Include]
        public Category Category { get; set; }

        [Association("ProductToOrders", "Id", "PRODID")]
        [Include]
        public EntityCollection<Order> Orders { get; set; }
    }
}

现在我们需要告诉 RIA 服务我们希望它加载关联: (注意:Intellisense 说它是一个点分隔的属性名称列表,但是我尝试了类似 .Include("Category.SubCategory") 之类的东西,但失败了一个异常......虽然 .Include("Category").Include( “SubCategory”)就像一个魅力!)

public IQueryable<Product> GetProducts() {
    return this.ObjectContext.Products.Include("Category");
}

我现在可以从 Silverlight 客户端访问我的“类别”属性,它不为 NULL :)

【讨论】:

  • 添加 [Association] 对我没有任何改变 (SL5)。
  • 当时我相信我正在创建一个使用 .edmx 和 POCO 类混合映射的库存。 IIRC的经历是一样的。很抱歉,自从我从事任何类似项目以来已经很长时间了,并且再也无法访问它了。请记住同时使用“Key”和“Association”属性,而不仅仅是“Association”,没有Key,关联无法解析。键通常是可索引的(数字)。关联有时可以基于“IsForeignKey”发挥作用的其他类中的键。
【解决方案3】:

与 SilverX 相同:只是遇到了问题,解决了它并认为它可能对某人有用。

我的所有配置都正确无误([Include] 用于 RIA S,Include() 用于 EF),但 Silverlight 端的导航属性仍然​​为空。

原来域服务方法使用了[Invoke] 属性(并返回IEnumerable&lt;T&gt;)。删除这个属性解决了这个问题。

(仅作记录,正在使用 [Invoke],因为该方法有一个 List&lt;Entity&gt; 参数)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多