English 中文(简体)
• 如何使用区域独立影响评估服务局加入2个新的相关的DTO?
原标题:How to insert 2 new related DTOs using RIA Service?

I am using RIA Service in our Silverlight application. Database entities are not directly exposed to a client but I have a set of POCO classes for it. Then in CRUD methods for these POCO classes they are converted to database entities and saved to database. The problem arises on the server side when client creates 2 new POCO entities which are related. Insert method is called on the server for each POCO entity separately and I may create corresponding new database entities there and add them to object context. But I see no way to add relation between these created database entities. Is there a solution for that?

例如,我有这2个组织实体(简化):

[DataContract(IsReference = true)]
public partial class Process
{
    [DataMember]
    [Key]
    public string Name
    {
       get; set;
    }

    [DataMember]
    public long StepId
    {
       get; set;
    }

    [DataMember]
    [Association("StepProcess", "StepId", "Id", IsForeignKey=true)]
    public Step Step
    {
       get; set;
    }
}

[DataContract(IsReference = true)]
public partial class Step
{
    [DataMember]
    [Key]
    public long Id
    {
       get; set;
    }

    [DataMember]
    public string Name
    {
       get; set;
    }
}

在我的领域服务类别中,我有这2种方法:

public void InsertProcess(Process process)
{
    var dbProcess = new DBProcess();
    dbProcess.Name = process.Name;
    //dbProcess.StepId = process.StepId;  Cannot do that!
    this.ObjectContext.AddToDBProcess(dbProcess);
}

public void InsertStep(Step step)
{
    var dbStep = new DBStep();
    dbStep.Name = step.Name;
    this.ObjectContext.AddToDBSteps(dbStep);

    this.ChangeSet.Associate<Step, DBStep>
            (step, dbStep, (dto, entity) =>
            {
                dto.Id = entity.Id;
            });
}

Client adds a new Process, then creates and adds a new Step to it and then calls SubmitChanges(). Process.StepId is not filled with a correct value as there is no correct Step.Id for the newly created step yet, so I cannot just copy this value to database entity. So the question is how to recreate relations between newly created database entities the same as they are in newly created DTOs?

我知道组成归属,但不适合我们。 进程和步骤都是独立的实体(即可能没有进程就存在步骤)。

问题回答

解决这一问题有两个途径:

  1. 每次打电话后,每打回物品的主要钥匙,就可以将由此而产生的PKey储存在另一个POCO中,称为第二个服务。

  2. 创立一种服务方法,将组织业务组织作为参数,并将其工作与你们联系起来。

Thanks, although both these suggestions are valid but they are also applicable only for simple and small object hierarchies, not my case. I end up using approach similar to this. I.e. I have a POCO to database objects map. If both Process and Step are new, in InsertProcess method process.Step navigation property is filled with this new step (otherwise StepId can be used as it referenced to existing step). So if this process.Step is in the map I just fill corresponding navigation property in DBProcess, otherwise I create new instance of DBStep, put it to the map and then set it to DBProcess.Step navigation property. This new empty DBStep will be filled in InsertStep method later.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签