是否可以将POCO实体与标准实体相关联?

时间:2012-05-16 18:53:15

标签: c# frameworks entity poco

假设我有一个名为SalesOrders的POCO实体和一个名为SalesOrderLines的SQL数据库生成的常规实体。我想创建一个从SalesOrders到SalesOrderLines的关联,如下面的代码,但我不断在代码下面获得异常。有谁知道这是否可行?

[DataServiceKey("SalesOrderNumber")]
public class SalesOrder
{
    [Key]
    public int SalesOrderNumber { get; set; }
    public string StockCode { get; set; }
    public string Description { get; set; }

    [Include]
    [Association("SalesOrder_SalesOrderLine", "SalesOrderNumber", "SalesOrderNumber")]
    public IQueryable<SalesOrderLine> SalesOrderLines
    {
        get
        {
            SalesOrderLineEntities oSalesOrderLineEntities = new SalesOrderLineEntities();
            var soLines = from line in oSalesOrderLineEntities.SalesOrderLines
                         where line.OrderNumber == SalesOrderNumber.ToString() &&
                            line.StockCode == StockCode
                         select line;
            return soLines;
        }
        set
        {
        }
    }
}

'类型'ServiceName.NameSpace.SalesOrder'上的属性'SalesOrderLines'不是有效属性。不支持其类型是基元或复杂类型集合的属性。'

1 个答案:

答案 0 :(得分:1)

事实证明,我需要做的就是将[Key]属性添加到生成的元数据类中。

[MetadataTypeAttribute(typeof(SalesOrderLines.SalesOrderLinesMetadata))]
public partial class SalesOrderLines
{

    internal sealed class SalesOrderLinesMetadata
    {

        private SalesOrderLinesMetadata()
        {
        }

        public DateTime DateStamp { get; set; }

        public string OrderNumber { get; set; }

        [Key]
        public int SalesOrderLineID { get; set; }

        public string SerialNumber { get; set; }

        public string StockCode { get; set; }
    }
}