4

VB MV4 プロジェクトで EntityFrameWork 5 を使用しています。

私はEntityFrameworkダイアグラムから構築されたデータベースを持っています(コードファーストではなくモデルファースト)

List(ofT) T を含む ViewModel X が私のエンティティの 1 つです。

(ブラウザーで) Web アプリケーションを開くと、Knockout JS Mapping プラグインを使用して MVVC (ノックアウト モデル) を設定するために使用する Json オブジェクトとして ViewModel X を提供するようコントローラーに要求します。

モデルを要求するときは、以下に示すようなコードを使用して入力します

Public Class DataServiceController
    Inherits System.Web.Mvc.Controller

    <Authorize()> _
    Public Function RetrieveData() As JsonResult

        Dim model As ViewModelX
        model = New ViewModelX

    '=====================================
    ' Resources and Tools
    '=====================================
    Dim fetchedResourceAndToolsQuery = From a In db.ResourceAndTools
                       Where a.ProfileId = profile.ProfileId Select a

    For Each eachRes In fetchedResourceAndToolsQuery
        Dim res As ResourceAndTools = New ResourceAndTools
        res.Name = Trim(eachRes.Name)
        res.URL = Trim(eachRes.URL)
        res.Target = eachRes.Target
        res.ResourceId = eachRes.ResourceId
        model.ResourceAndTools.Add(res)
    Next

    Return Json(model, JsonRequestBehavior.AllowGet)

すべてがうまく機能します!を除いて...そしてここに質問があります

上で述べたように、ViewModelX にはResourceAndToolsである List(of T) T が含まれています。

fetchedResourceAndToolsQuery (Linq クエリの結果) の内容を model.ResourceAndTools (List(of T)) にコピー (クローン、ロード、用語がわからない) する必要がありましたが、新しいオブジェクトをインスタンス化し、プロパティをコピーする必要はありませんやってます。

検索しました(クローニング、ディープコピー、シャローコピーなど)解決策に最も近いのは、オブジェクトをディープコピーする方法についての説明でしたが、シリアライゼーションに依存していました。エンティティのすべてのプロパティではないため、機能しませんでしたフレームワーク オブジェクトはシリアライズ可能です。

どんな助けでも大歓迎ですありがとう

4

1 に答える 1

3

このような?

model.ResourceAndTools = (
    From a In db.ResourceAndTools
    Where a.ProfileId = profile.ProfileId
    Select New ResourceAndTools() With { _
        .Name = Trim(a.Name), _
        .URL = Trim(a.URL), _
        .Target = a.Target, _
        .ResourceId = a.ResourceId}).ToList()

あなたのコメントに従って、おそらくあなたはできるでしょう、

Dim dataList = (
    From a In db.ResourceAndTools
    Where a.ProfileId = profile.ProfileId
    Select New With
    {
        .Name = Trim(a.Name),
        .URL = Trim(a.URL),
        .Target = a.Target,
        .ResourceId = a.ResourceId
    }).ToList()

model.ResourceAndTools = dataList.ConvertAll(Function(data)
        Return New ResourceAndTools() With
        {
            .Name = data.Name,
            .Url = data.Url,
            .Target = data.Target,
            .ResourceId = data.ResourceId
        }
    End Function)
于 2013-09-17T16:55:11.583 に答える