POCO DomainService 和 Client DomainContext EntityCollections 缺少实体

发布于 2024-10-02 06:20:58 字数 4172 浏览 4 评论 0原文

我有一个 MainBusinessLine 类型,其中包含 Group 类型的 ObservableCollection,并且我从 DomainService 调用的方法返回 MainBusinessLines 的可查询列表。因此,在此方法中,我已确认所有适当的组都已添加到 MainBusinessLines Groups 集合中。但是,当实体加载到客户端 silverlight 应用程序中时,几乎所有实体都丢失了。但更重要的是,MainBusinessLine 有另一个集合,并且在客户端加载操作后它确实包含所有适当的实体。我的关联属性也是如此,这意味着 thisKey 和 otherKey 的参数设置类似。

我还创建 POCO 类,而不是使用 EntityFramework。这是我的 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");
        }
    }
}

这是 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");  
        }  
    }  
}  

有人可以告诉我这里可能有什么问题吗?我感觉这与我设置关联属性的方式有关。谢谢

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. So in this method I have confirmed that all the appropriate Groups are added to the MainBusinessLines Groups collection. But when the entities are loaded in the client silverlight application it's missing pretty much all of them. But the kicker is that the MainBusinessLine has another collection and it does contain all the appropriate entities after the client load operation. And my Association Attributes are the same way, meaning the parameters for thisKey and otherKey are set similar.

I am also creating POCO classes instead of using the EntityFramework. Here is a sample of my MainBusinessLine class:

[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:

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2024-10-09 06:20:58

好吧,所以我终于弄清楚我做错了什么。因此,我将简要而详细地解释如何设置“POCO DomainService”。我确信这不是唯一的方法,但这是我发现的最简单的方法,而且有效。希望这能为其他人省去一周的头痛和挫败感。

基本上,我的情况是我有一个 MainBusinessLine 类型,它有一个 Groups 集合和一个 DefaultLOBs 集合。但我没有包括的是我有一个 GroupsReference 类型,它有一个 MainBusinessLineID 属性和一个 GroupID 属性。当我检索数据时,我继续循环遍历 GroupsReference 类型,并将组添加到父 MainBusinessLine 的 GroupsCollection 中。对于 DefaultLOBsCollection 也是如此。因此,在从 DomainService 中“查询”集合之前,我正在填充集合,这就出现了问题。

经过思考并阅读了我能找到的所有资料后,编码之神终于怜悯我并给了我答案。我将检索数据并创建每种类型的列表,填充任何作为关联的集合属性。然后在适当的查询方法中返回列表 AsQuerable(),并且在客户端上生成的实体将其 EntityCollections 填满。

因此总而言之,如果您使用正确的键设置了“包含”和“关联”属性,并且不尝试强制数据,那么您应该会成功。我将包含我的新示例代码,希望这对其他人有帮助。

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

再一次,我所做的就是从数据库中获取数据。使用反射创建每种类型的实例,设置属性值(不填充任何集合),并将它们添加到列表中。然后将列表返回给客户端。剩下的全部由生成的代码完成。

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". 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. But what I didn't include was that I had a GroupsReference type, that had a MainBusinessLineID property and a GroupID property. When I retrieved my data I went ahead and looped through the GroupsReference types and added the Groups to the parent MainBusinessLine's GroupsCollection. And the same thing for the DefaultLOBsCollection as well. So I was populating the collection before they were being "Queried" from the DomainService, and there was the issue.

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.

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.

莫相离 2024-10-09 06:20:58

public ObservableCollection上的 XmlIgnoreSoapIgnore 属性是否会起作用? DefaultLOBs 属性是您看不到“MainBusinessLines”集合的原因吗?

Would the XmlIgnore and SoapIgnore attributes on your public ObservableCollection<LOBs> DefaultLOBs property be the reason you're not seeing your "MainBusinessLines" collection?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文