3

私はこのリンクをたどりますASP.NET Web APIでODataアクションをサポートしています そして、私は自分のオブジェクト/エンティティを次のようなパラメーターとして渡したいです:

ActionConfiguration addNewPatient = builder.Entity<Patient>().Collection.Action("AddNewPatient");
        addNewPatient.Parameter<int>("hospId");
        addNewPatient.Parameter<int>("docId");
        addNewPatient.Parameter<Patient>("patient");
        addNewPatient.Returns<bool>();

しかし、私はこの問題を抱えています:

System.ArgumentException: Invalid parameter type 'Patient'. 
A non-binding parameter type must be either Primitive, Complex, Collection of Primitive or a Collection of Complex.
Parameter name: parameterType

私はこれを実装しようとしました

   ActionConfiguration addNewPatient = builder.Entity<Patient>().Collection.Action("AddNewPatient");
    addNewPatient.Parameter<int>("hospId");
    addNewPatient.Parameter<int>("docId");
    var patientConfig = builder.StructuralTypes.OfType<EntityTypeConfiguration>().Single(x => x.Name == "Patient");
    addNewPatient.SetBindingParameter("patient", patientConfig, false);
    addNewPatient.Returns<bool>();

しかし、メソッド POST ../odata/Patient/AddNewPatient を呼び出すことはもうできません

<FunctionImport Name="AddNewPatient" ReturnType="Edm.Boolean"      IsBindable="true">
<Parameter Name="patient" Type="Patient"/>
<Parameter Name="hospId" Type="Edm.Int32" Nullable="false"/>
<Parameter Name="docId" Type="Edm.Int32" Nullable="false"/>
</FunctionImport>

私を助けてください、私はさまざまな方法を試しましたが、まだ運がありません。ありがとう。

4

2 に答える 2

3

ActionConfiguration.EntityParameter() メソッドを使用して、エンティティをパラメーターとして OData アクション メソッドにバインドできます。

次に例を示します。

ActionConfiguration validate = ModelBuilder.EntityType<TEntity>()
    .Collection.Action("Validate");
validate.Namespace = "Importation";
validate.EntityParameter<TEntity>(typeof(TEntity).Name);
validate.CollectionParameter<string>("UniqueFields");
validate.Returns<ValidationResult>();

ただし、ModelState は提供されたエンティティのコンテンツをチェックせず、欠落しているプロパティを null に設定し、モデルの StringLength(x) アノテーションを超えるプロパティは引き続き通過することに注意してください。後でエンティティ自体を検証する場合は、アクション メソッドで次のコードを使用します。

[HttpPost]
public virtual IHttpActionResult Validate(ODataActionParameters parameters)
{
//First we check if the parameters are correct for the entire action method
    if (!ModelState.IsValid)
    {
         return BadRequest(ModelState);
    }
    else
    {
         //Then we cast our entity parameter in our entity object and validate
         //it through the controller's Validate<TEntity> method
         TEntity Entity = (TEntity)parameters[typeof(TEntity).Name];
         Validate(Entity, typeof(TEntity).Name);
         if (!ModelState.IsValid)
         {
              return BadRequest(ModelState);
         }
         IEnumerable<string> uniqueFields = parameters["UniqueFields"] as IEnumerable<string>;
         bool result = Importer.Validate(Entity, uniqueFields);
         return Ok(result);
    }
}
于 2015-11-18T22:25:06.730 に答える
0

新しい患者オブジェクトを /odata/Patient に POST するだけの方がよいのではないでしょうか? そのためにあるのです。

説明した方法で実行したい場合は、中間型を作成し、その型のパラメーターを作成してから、それと Edm 型の間で変換する必要があります。

var createPatient = modelBuilder.Entity<Patient>().Collection.Action("AddNewPatient");
createPatient.CollectionParameter<PatientPdo>("patient");

ここで、PatientPdo は、ナビゲーション プロパティが削除されているだけで、Patient とまったく同じです。いわば、Edm 型であるということです。

public class PatientPdo
    {
        public long Id{ get; set; }

        public Entity ToEdmEntity()
        {
            return new Patient
                {
                    Id= Id
                };
        }
    }
于 2013-12-31T11:31:12.757 に答える