2017-08-08 104 views
0

我有一個類:如何手動配置EF Core中屬性的數據庫類型?

class Order 
{ 
    public OrderId Id {get;set;} 
} 

struct OrderId 
{ 
    private int readonly id; 

    public OrderId(int id) 
    { 
     this.id = id; 
    } 
} 

出於某種原因,我不希望使用原始類型的屬性。 當我試圖通過Entity Framework Core保存這個類Sql Server的對象,我得到一個錯誤:

System.InvalidOperationException: 'The property 'Order.Id' is of type 'OrderId' which is not supported by current database provider. Either change the property CLR type or manually configure the database type for it.'

問題:是否有可能配置映射在這種情況下怎麼辦呢?

+0

你可能要檢查這個https://stackoverflow.com/questions/19370104/convert -value-when-mapping –

+0

我不想使用其他字段。感謝您的建議。 – Alew

+0

@Alew你可以嘗試指定一個明確的數據類型,但是你還需要定義一個轉換運算符,並且在你付出所有努力之後,我懷疑它會起作用。查詢看起來如何試圖在這種非原始類型上匹配。爲了指定查詢條件,你必須爲結構添加新的元素。實體需要非常接近他們的數據庫定義,包括匹配數據庫類型。最好映射到模型或在需要時使用其他屬性。我真的會坐下來思考,「某種原因」是你認爲一種原始類型是不夠的。 – AaronLS

回答

相關問題