简体   繁体   English

POCO DomainService和Client DomainContext EntityCollection缺少实体

[英]POCO DomainService and Client DomainContext EntityCollections are missing Entities

I have a MainBusinessLine type with an ObservableCollection of type Group, and a method that I call from a DomainService the returns a queryable list of MainBusinessLines. 我有一个MainBusinessLine类型,其ObservableCollection类型为Group,我从DomainService调用了一个方法,该方法返回MainBusinessLines的可查询列表。 So in this method I have confirmed that all the appropriate Groups are added to the MainBusinessLines Groups collection. 因此,在这种方法中,我已经确认所有适当的组都已添加到MainBusinessLines组集合中。 But when the entities are loaded in the client silverlight application it's missing pretty much all of them. 但是,当实体加载到客户端silverlight应用程序中时,几乎所有实体都丢失了。 But the kicker is that the MainBusinessLine has another collection and it does contain all the appropriate entities after the client load operation. 但是最重​​要的是MainBusinessLine还有另一个集合,并且在客户端加载操作之后它确实包含所有适当的实体。 And my Association Attributes are the same way, meaning the parameters for thisKey and otherKey are set similar. 而且我的“关联属性”是相同的方式,这意味着thisKey和otherKey的参数设置相似。

I am also creating POCO classes instead of using the EntityFramework. 我也在创建POCO类,而不是使用EntityFramework。 Here is a sample of my MainBusinessLine class: 这是我的MainBusinessLine类的示例:

[Serializable,
DataContract]
public class MainBusinessLine : BaseDataContract
{
    public MainBusinessLine()
    {

    }

    private int _MainBusinessLineID;
    [Key,
    DataMember,
    Required]
    public int MainBusinessLineID
    {
        get
        {
            return _MainBusinessLineID;
        }
        set
        {
            if (_MainBusinessLineID == value)
                return;

            ReportPropertyChanging("MainBusinessLineID");
            _MainBusinessLineID = value;
            ReportPropertyChanged("MainBusinessLineID");
        }
    }

    private ObservableCollection<LOBs> _DefaultLOBs;
    [DataMember,
    XmlIgnore,
    SoapIgnore,
    Include,
    Association("DefaultLOBsCollection", "MainBusinessLineID", "MainBusinessLineID")]
    public ObservableCollection<LOBs> DefaultLOBs
    {
        get
        {
            return _DefaultLOBs;
        }
        set
        {
            ReportPropertyChanging("DefaultLOBs");
            _DefaultLOBs = value;
            ReportPropertyChanged("DefaultLOBs");
        }
    }



    private ObservableCollection<Groups> _Groups;
    [DataMember, 
    Include,
    Association("GroupsCollection", "MainBusinessLineID", "MainBusinessLineID")]
    public ObservableCollection<Groups> Groups
    {
        get
        {
            return _Groups;
        }
        set
        {
            ReportPropertyChanging("Groups");
            _Groups = value;
            ReportPropertyChanged("Groups");
        }
    }
}

And here are the LOBs and Groups classes: 这是LOB和Groups类:

[Serializable,
DataContract]
public class LOBs : BaseDataContract
{
    private int _LOBID;
    [Key,
    DataMember,
    Required]
    public int LOBID
    {
        get
        {
            return _LOBID;
        }
        set
        {
            if (_LOBID == value)
                return;

            ReportPropertyChanging("LOBID");
            _LOBID = value;
            ReportPropertyChanged("LOBID");
        }
    }

    private int _GroupID;
    [DataMember]
    public int GroupID
    {
        get
        {
            return _GroupID;
        }
        set
        {
            if (_GroupID == value)
                return;

            ReportPropertyChanging("GroupID");
            _GroupID = value;
            ReportPropertyChanged("GroupID");
        }
    }  

    private int _MainBusinessLineID;
    [DataMember]
    public int MainBusinessLineID
    {
        get
        {
            return _MainBusinessLineID;
        }
        set
        {
            if (_MainBusinessLineID == value)
                return;

            ReportPropertyChanging("MainBusinessLineID");
            _MainBusinessLineID = value;
            ReportPropertyChanged("MainBusinessLineID");
        }
    }  
}  

[Serializable,
DataContract]
public class Groups : BaseDataContract
{
    private int _GroupID;
    [Key,
    DataMember,
    Required]
    public int GroupID
    {
        get
        {
            return _GroupID;
        }
        set
        {
            if (_GroupID == value)
                return;

            ReportPropertyChanging("GroupID");
            _GroupID = value;
            ReportPropertyChanged("GroupID");
        }
    }

    private int _MainBusinessLineID;
    [DataMember]
    public int MainBusinessLineID
    {
        get
        {
            return _MainBusinessLineID;
        }
        set
        {  
            if (_MainBusinessLineID == value)  
                return;  

            ReportPropertyChanging("MainBusinessLineID");  
            _MainBusinessLineID = value;  
            ReportPropertyChanged("MainBusinessLineID");  
        }  
    }  
}  

Could someone please tell me what I may have wrong here? 有人可以告诉我这里可能有什么问题吗? I have a feeling it has something to do with the way I'm setting the association attributes. 我觉得这与我设置关联属性的方式有关。 Thanks 谢谢

Ok, so I finally figured out what I was doing wrong. 好的,所以我终于弄清楚我做错了什么。 So I'm going to give a brief, yet detailed, explanation of how to set up a "POCO DomainService". 因此,我将简要介绍如何设置“ POCO DomainService”。 I'm sure this is not the only way, but it's the simplest way I found, and it works. 我敢肯定这不是唯一的方法,但这是我发现的最简单的方法,并且有效。 And hopefully this will save someone else a weeks worth of headaches and frustrations. 希望这将为他人节省数周的头痛和挫败感。

So basically, my situation was that I had a MainBusinessLine type that had a Groups collection and a DefaultLOBs collection. 所以基本上,我的情况是我有一个MainBusinessLine类型,它具有一个Groups集合和一个DefaultLOBs集合。 But what I didn't include was that I had a GroupsReference type, that had a MainBusinessLineID property and a GroupID property. 但是我没有包括的是我有一个GroupsReference类型,它具有MainBusinessLineID属性和GroupID属性。 When I retrieved my data I went ahead and looped through the GroupsReference types and added the Groups to the parent MainBusinessLine's GroupsCollection. 检索数据时,我继续浏览了GroupsReference类型,然后将Groups添加到父MainBusinessLine的GroupsCollection中。 And the same thing for the DefaultLOBsCollection as well. 同样,DefaultLOBsCollection也是如此。 So I was populating the collection before they were being "Queried" from the DomainService, and there was the issue. 因此,在从DomainService中“查询”集合之前,我正在填充集合,这就是问题所在。

After thinking about it and reading all I could find, the Gods of Coding finally took pity on me and struck me with the answer. 经过思考并阅读了所有我能找到的内容后,编码之神终于可怜我,并给了我答案。 I would retrieve the data and create Lists of each type WITHOUT populating any of the collection properties that are associations. 我将检索数据并创建每种类型的列表,而无需填充任何关联的集合属性。 And then return the Lists AsQuerable() in their appropriate Query Method, and the entities that are generated on the client will have their EntityCollections full. 然后以其适当的查询方法返回List AsQuerable(),并且在客户端上生成的实体将具有完整的EntityCollections。

So in conclusion, if you have the Include and Association attributes set with the correct keys and not try to force the data, then you should have success. 因此,总而言之,如果您使用正确的键设置了“包含”和“关联”属性,而没有尝试强制使用数据,那么您应该会成功。 I'll include my new sample code and hopefully this will helpful for someone else. 我将包含新的示例代码,希望这对其他人有帮助。

[Serializable,  
DataContract]  
public class MainBusinessLine : BaseDataContract  
{  
    Int32 _MainBusinessLineID;
    [Key,
    DataMember,
    Required]
    public Int32 MainBusinessLineID
    {
        get
        {
            return _MainBusinessLineID;
        }
        set
        {
            if (_MainBusinessLineID == value)
                return;

            _MainBusinessLineID = value;
            ReportPropertyChanged("MainBusinessLineID");
        }
    }

    ObservableCollection<GroupsReference> _GroupsReferenceCollection;
    [DataMember,
    Include,
    Association("GroupsReferenceCollection", "MainBusinessLineID", "MainBusinessLineID")]
    public ObservableCollection<GroupsReference> GroupsReferenceCollection
    {
        get
        {
            return _GroupsReferenceCollection;
        }
        set
        {
            _GroupsReferenceCollection = value;
            ReportPropertyChanged("GroupsReferenceCollection");
        }
    }
}

[Serializable,  
DataContract]  
public class GroupsReference : BaseDataContract
{
    Int32 _GroupsReferenceID;
    [Key,
    DataMember,
    Required]
    public Int32 GroupsReferenceID
    {
        get
        {
            return _GroupsReferenceID;
        }
        set
        {
            if (_GroupsReferenceID == value)
                return;

            _GroupsReferenceID = value;
            ReportPropertyChanged("GroupsReferenceID");
        }
    }

    Int32 _MainBusinessLineID;
    [DataMember,
    Required]
    public Int32 MainBusinessLineID
    {
        get
        {
            return _MainBusinessLineID;
        }
        set
        {
            if (_MainBusinessLineID == value)
                return;

            _MainBusinessLineID = value;
            ReportPropertyChanged("MainBusinessLineID");
        }
    }

    Int32 _GroupID;
    [DataMember,
    Required]
    public Int32 GroupID
    {
        get
        {
            return _GroupID;
        }
        set
        {
            if (_GroupID == value)
                return;

            _GroupID = value;
            ReportPropertyChanged("GroupID");
        }
    }
}  

[Serializable,  
DataContract]  
public class Group : BaseDataContract  
{  
    Int32 _GroupID;
    [Key,
    DataMember,
    Required]
    public Int32 GroupID
    {
        get
        {
            return _GroupID;
        }
        set
        {
            if (_GroupID == value)
                return;

            _GroupID = value;
            ReportPropertyChanged("GroupID");
        }
    }

    ObservableCollection<GroupsReference> _GroupsReferenceCollection;
    [DataMember,
    Include,
    Association("GroupsReferenceCollection", "GroupID", "GroupID")]
    public ObservableCollection<GroupsReference> GroupsReferenceCollection
    {
        get
        {
            return _GroupsReferenceCollection;
        }
        set
        {
            _GroupsReferenceCollection = value;
            ReportPropertyChanged("GroupsReferenceCollection");
        }
    }
}  

[EnableClientAccess(RequiresSecureEndpoint = false)]
public class DentalAdminPortalDomainService : DomainService
{

    public DentalAdminPortalDomainService()
    {
        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    }


    [Query]
    public IQueryable<MainBusinessLine> GetMainBusinessLines()
    {
        return DataRepository.GetMainBusinessLines().AsQueryable<MainBusinessLine>();
    }

    [Query]
    public IQueryable<Groups> GetGroups()
    {
        return DataRepository.GetGroups().AsQueryable<Groups>();
    }

    [Query]
    public IQueryable<GroupLOBList> GetGroupsReference()
    {
        return DataRepository.GetGroupsReferences().AsQueryable<GroupsReference>();
    }  
}  

And once again, all I do is get the data from the database. 再一次,我要做的就是从数据库中获取数据。 Use reflection to create instances of each type, set the property values (no collections are filled), and add them to a list. 使用反射创建每种类型的实例,设置属性值(不填充任何集合),然后将它们添加到列表中。 Then return the lists to the client. 然后将列表返回给客户端。 The rest is all done by the generated code. 其余的全部由生成的代码完成。

您的public ObservableCollection<LOBs> DefaultLOBs属性上的XmlIgnoreSoapIgnore属性是否是您看不到“ MainBusinessLines”集合的原因?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM