2012-05-16 122 views
1

假設我必須實體名爲SalesOrders的POCO實體和從名爲SalesOrderLines的SQL數據庫生成的常規實體。我想創建一個從SalesOrders到SalesOrderLines的關聯,如下面的代碼所示,但是我一直在代碼下面看到異常。有誰知道這是否可能?是否可以將POCO實體與標準實體相關聯?

[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

事實證明,我需要做的就是將[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; } 
    } 
}