![](/img/trans.png)
[英]How to use DbGeography.Filter in Linq with Entity Framework 5?
[英]Not able to use Nullable DbGeography
我在SQL中有一個地理字段,我將其標記為null。
然后在我的EF實體上我添加了一個可以為空的DbGeography?
財產,但我收到一條消息說不允許。
那么,如何在數據庫中插入未定義的DbGeography?
我正在尋找像DbGeography.Empty或DbGeography.Unknown這樣的東西。
但是我找不到任何......
謝謝你,米格爾
DbGeography是一個類,所以它已經可以為空了。 只需在模型中給它一個空值。
如果你想要一個非null的DbGeography
屬性,但想表達一個空(但不是null)的值,你可以這樣做......
[Required] public virtual DbGeography GpsCoordsOrWhatever { get; set; } = DbGeography.FromText("POINT EMPTY")
DbGeography
和System.Data.Entity.Spatial
命名空間中的其他類型是類(引用類型,因此可以為null)。
相反,如果你想擁有class-type的非null modelBuilder.Entity<Entity>().Property(t => t.Property).IsRequired()
屬性,你可以將[Required]
屬性應用於屬性,或者使用modelBuilder.Entity<Entity>().Property(t => t.Property).IsRequired()
。 這不會阻止對屬性的null
賦值,但它將映射到后備存儲中的非null -able字段(大多數情況下為數據庫),並且將在Entity Framework的驗證中檢查null
。
這是因為DbGeography是類,並且不允許將標記類作為nullable
(可以為nullable
東西僅用於結構)。
將您的房產類型更改為DbGeography
(而不是DbGeography?
)。 由於DbGeography是類,因此屬性可以具有null
值。
實體不需要明確標記為可為空。 無論如何,您將能夠將其設置為null。
你可以做MyDbGeographyProperty = null;
緯度和經度是從obj只讀的,這是kjbartel所說的;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.