【问题标题】:odata breeze: Collection of ComplexType: - "TypeError: Cannot call method '_createInstanceCore…"odata 微风:ComplexType 的集合:-“TypeError:无法调用方法'_createInstanceCore ...”
【发布时间】:2014-02-09 23:44:39
【问题描述】:

我想使用 Breeze 来处理 OData 服务。我花了几个小时试图让它运行,但我没有找到解决方案,所以我真的希望有人能提供帮助!? (我刚开始使用微风 - 所以如果我在这里遗漏了一些“明显”的东西,我很抱歉。)

我想做的是使用子对象的集合(ComplexType)。 IE。我希望我的实体类具有这样的集合属性:

public List<Address> addresses { get; set; }

(地址不是在数据库集合中自行持久化的实体,而是具有一些属性(ComplexType)的简单类)

服务器是一个 MVC4 WebAPI ODATA 服务,它基于持久化在 MongoDB 数据库中的实体框架代码优先 POCO 类。

在浏览器中我收到以下错误,当微风库尝试从服务器读取 ODATA 元数据时(它甚至不查询示例集合,在此之前它有一个错误):

[Q] Unhandled rejection reasons (should be empty): 
["TypeError: Cannot call method '_createInstanceCore…://localhost:56936/scripts/breeze.debug.js:236:26"]

有什么方法可以让它运行吗?

谢谢!

我的客户端代码如下所示:

var serverAddress = "http://localhost:56936/odata";
breeze.config.initializeAdapterInstances({ dataService: "OData" });
var manager = new breeze.EntityManager(serverAddress);

var query = breeze.EntityQuery.from("BreezeExample");
manager.executeQuery(query).then(querysucceeded).fail(queryfailed);

function querysucceeded(data) {
    console.log("querysucceeded");
    data.results.forEach(function (item) {
        //...
    });
}

function queryfailed(e) {
    console.log("!!! queryfailed");
    console.log(e);
}

型号如下:

[BsonIgnoreExtraElements]
public class ExampleClass : MongoEntity
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string loginEmail { get; set; }
    public bool? active { get; set; }
    public ICollection<Address> addresses { get; set; } //THIS DOES NOT WORK CLIENT SIDE - WHY?
}

public class Address
{
    public string place { get; set; }
    public string street { get; set; }
    public string houseNumber { get; set; }
    public string postalCode { get; set; }
}

如果我使用浏览器 (http://localhost:56936/odata/$metadata) 查询 ODATA 元数据,我会得到以下结果:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
  <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0">
    <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="MvcWebRole1.Models.MongoDB.Entities">
      <EntityType Name="ExampleClass">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="loginEmail" Type="Edm.String" />
        <Property Name="active" Type="Edm.Boolean" />
        <Property Name="addresses" Type="Collection(MvcWebRole1.Models.MongoDB.Entities.Address)" Nullable="false" />
        <Property Name="Id" Type="Edm.String" Nullable="false" />
      </EntityType>
      <ComplexType Name="Address">
        <Property Name="place" Type="Edm.String" />
        <Property Name="street" Type="Edm.String" />
        <Property Name="houseNumber" Type="Edm.String" />
        <Property Name="postalCode" Type="Edm.String" />
      </ComplexType>
      <EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
        <EntitySet Name="BreezeExample" EntityType="MvcWebRole1.Models.MongoDB.Entities.ExampleClass" />
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

如果我查询 BreezeExample 集合 (http://localhost:56936/odata/BreezeExample),我会得到:

{
    odata.metadata: "http://localhost:56936/odata/$metadata#BreezeExample",
    value: [
    {
        loginEmail: "loginEmail1",
        active: true,
        addresses: [
        {
            place: "place",
            street: "street",
            houseNumber: "77",
            postalCode: "123"
        },
        {
            place: "place2",
            street: "street",
            houseNumber: "77",
            postalCode: "123"
        }
        ],
        Id: "1"
    },
    {
        loginEmail: "loginEmail2",
        active: true,
        addresses: [
        {
            place: "place",
            street: "street",
            houseNumber: "77",
            postalCode: "123"
        },
        {
            place: "place",
            street: "street",
            houseNumber: "77",
            postalCode: "123"
        }
        ],
        Id: "2"
    }
    ]
}

【问题讨论】:

    标签: entity-framework asp.net-mvc-4 odata breeze complextype


    【解决方案1】:

    我设法通过执行以下操作部分解决了问题:

    首先,事实证明Breeze不理解Collection(MvcWebRole1.Models.MongoDB.Entities.Address)类型;因此它无法创建适当的dataType 属性。这可能是您收到错误的原因。 我将类型更改为 Edm.Self.Address 之类的东西。

    我所做的另一项更改是在parseCsdlComplexProperty 函数中。我又添加了一行来解决所有问题。

        var dp = new DataProperty({
            nameOnServer: csdlProperty.name,
            complexTypeName: complexTypeName,
            isScalar: csdlProperty.isScalar, // LINE ADDED
            isNullable: false
        });
    

    回顾一下,我的元数据文件包含以下 sn-p

    {
        "name": "ColourByType",
        "key": {
          "propertyRef": {
            "name": "Id"
          }
        },
        "property": [
          {
            "name": "Id",
            "type": "Edm.Int32",
            "nullable": "false",
            "annotation:StoreGeneratedPattern": "Identity"
          },
          {
            "name": "Options", // This is the list that holds the instance of my complex type
            "type": "Edm.Self.ColourByOption", // This is the complex type that is stored in the list
            "isScalar": false 
          }
        ]
      }
    

    我确信这个解决方案并不理想,但它对我有用。

    我问过一个类似的问题;所以你可能也想继续关注它

    https://stackoverflow.com/questions/21258015/breezejs-metadata-is-missing-information-about-complex-properties

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 2015-01-21
    相关资源
    最近更新 更多