リアサービス:複数の挿入に着目したプレゼンテーションモデルオブジェクト

StackOverflow https://stackoverflow.com/questions/2166213

質問

私共有データをリアを用いたサービスの発表モデルのLINQ to SQLます。のSilverlightアで作られたカップルの新しい主体のアルバムやアーティスト)の関連で、うその他のいずれかの追加のアルバムのアーティストのアルバムに収集、または設定の作物のアルバムは作品を追加しているコンテキストを提出します。

サーバにとって二つの別々の挿入電話-のアルバムのアーティスト。これらの者は新しいID値を定めるとともに、デフォルトのint値(0-意することによって自分のDBることができる有効なIDのDB)で調査を実施しているのは、同じかんセットIdのための新しい組織にするサービスです。こう作ればまた移転にLINQ to SQLの授業によっRIAサービスでも、アルバムを挿入し、作家とアーティストの挿入を含むアルバムには、主体のL2S文脈を認識しました。しかし、私の発表カスタムモデルオブジェクト、変換する必要があるセットのカバーを目標としていLINQ to SQLク 維持会の うに追加することができL2S。

一言で言えば、調査を実施しているのは、同じように、これは不可能である。各エンティティが自身の挿話ものができるために挿入し一体でなくIdの団体は失われます。場合は、データベース利用ガイド識別子で異なる物語が果たすことができるのであろうかいたものです。

こんなことが可能なので、はっきを追う。

役に立ちましたか?

解決

作成した場合は、正しい親子組んなトラックの発表の挿入モデル(PM)-団体との関係:

PM:

public class Parent
{
    [Key]
    public int? ParentID { get; set; }

    [Include]
    [Composition]
    [Association("Parent_1-*_Child", "ParentID", "ParentID", IsForeignKey = false)]
    public IEnumerable<Child> Children { get; set; }
}

public class Child
{
    [Key]
    public int? ChildID { get; set; }

    [Include]
    [Association("Parent_1-*_Child", "ParentID", "ParentID", IsForeignKey = true)]
    public Parent Parent { get; set; }
}

必ず使用してください[構成]をWCFリアのInsertChild法のDomainService.

Silverlight:

...
public Child NewChild(Parent parent)
{
    return new Child
                {
                    ParentID = parent.ParentID,
                    Parent = parent,
                };
}
...
public void SubmitChanges()
{
    DomainContext.SubmitChanges(SaveComplete, null);
}
...

場合は、親会社は新しくはないですが、いParentID.であれば、親会社IDはnullになります。設定によります。親会への参照を新しい親RIA理解しようとしているのな保存し、参照した後、サーバに送ります。

DomainServiceのサーバー:

[EnableClientAccess]
public class FamilyDomainService : DomainService
{
    private readonly IDictionary<object, EntityObject> _insertedObjectMap;

    public void InsertParent(Parent parent)
    {
        ParentEntity parentEntity = new ParentEntity();

        ObjectContext.AddToParents(parentEntity);
        _insertedObjectMap[parent] = parentEntity;

        ChangeSet.Associate(parent, parentEntity, (p, e) => p.ParentID = e.ParentID;
    }

    public void InsertChild(Child child)
    {
        var childEntity = new ChildEntity();

        if (child.ParentID.HasValue) // Used when the Parent already exists, but the Child is new
        {
            childEntity.ParentID = child.ParentID.GetValueOrDefault();
            ObjectContext.AddToChildren(childEntity);
        }
        else // Used when the Parent and Child are inserted on the same request
        {
            ParentEntity parentEntity;
            if (child.Parent != null && _insertedObjectMap.TryGetValue(child.Parent, out parentEntity))
            {
                parentEntity.Children.Add(childEntity);
                ChangeSet.Associate(child, childEntity, (c, e) => c.ParentID = e.Parent.ParentID);
            }
            else
            {
                throw new Exception("Unable to insert Child: ParentID is null and the parent Parent cannot be found");
            }
        }

        _insertedObjectMap[child] = childEntity;

        ChangeSet.Associate(child, childEntity, (c, e) => c.ChildID = e.ChildID );
    }

    protected override bool PersistChangeSet()
    {
        ObjectContext.SaveChanges();
        _insertedObjectMap.Clear();
        return true;
    }
}

二つの重要な点です。第一に、'_insertedObjectMap店舗との関係に新しく挿入された主体がないのIDをセット。しておりますので、その取引により、単一のDBのIDを設定することはすべて主体にして挿入されます。による保存の関係は、子どもPMので、企業の親会社のPMを使用データベースです。の子ども主体の子どものコレクションの親会社およびLINQToSQLはLINQToEnityFrameworkを処理する必要がありますの外部キーをドロップします。

第二の作品が関連付けの変更後取引している状態です。シナリオの親子の両方を提出し、決して忘れてはいけないと思設定ParentID外部キーの子供です。

Myインからチェンジセットから表示します。准教授()から: http://blogs.msdn.com/deepm/archive/2009/11/20/wcf-ria-services-presentation-model-explained.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top