0%

MongoDb EntityFramework Provider 更新之後要改好多東西

  1. IList >> List
  2. BsonDateTime >> DateTime

強制要改,否則實體會炸開來,
最後

  1. List<string>不可用,出現 list<string> is not a supported primitive type or a valid entity type

原生的EntityFramework的限制的樣子
感謝鮑大人幫我找到解法
StackOverflow : How to persist a list of strings with Entity Framework Core?
利用 Entity Framework Core 2.1Value Conversions 可解

比較之前的 EF 就只能 List property 掛個 [Notmapped] 然後配合用 Json.NET 或 Automapper 把顯示出來的和存起來的做轉換

  1. Dictionary<string, MyObject>也不可用,出現同3的錯誤

一樣利用 Value Conversions 可解
Jerrie Pelser : Store a Dictionary as a JSON string using EF Core 2.1

MongoDB

MongoDb Host

The mongo Shell

MongoDb Index

雷伊的工作心得 : [mongoDB]index功能的筆記

EntityFrameworkCore MongoDb Provider

更新之後dbcontext都壞了
今天終於找到原因
我之前都用BsonDateTime存日期
現在Provider改用DateTime去存
然後就爆炸了

另一點是不能使用IList做為宣告

1
because it is of type 'IList<string>' which is not a supported primitive type or a valid entity type.

List似乎也是

1
because it is of type 'List<string>' which is not a supported primitive type or a valid entity type.

好像直接用這個當 CosmosDB BravoPoint 設定即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Range",
"dataType": "String",
"precision": -1
}
]
},
{
"path": "/Location/?",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Spatial",
"dataType": "Point"
},
{
"kind": "Range",
"dataType": "String",
"precision": -1
}
]
}
],
"excludedPaths": [
{
"path": "/\"_etag\"/?"
}
]
}

MongoDB 的一些 Script

insert for all data
db.BravoEventPoints.update({}, {$set : {“StartTime” : new ISODate(“2012-01-10”)} }, {upsert:false, multi:true})
StackOverflow : Mongodb Add new field to every document

// update for all data
db.BravoEventPoints.update({}, {$set: {“EndTime” : null }}, {multi:true})
db.BravoEventPoints.updateMany({},{$set: {“StartTime” : null }})
StackOverflow : Mongodb update all

Azure CosmosDB 使用 MongoDB GeoLocation功能

真D好難

首先先到Azure CosmosDb 你的Collection設定內
將includedPaths改成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Range",
"dataType": "String",
"precision": -1
}
]
}

然後再用程式碼加入2dsphere的index

1
2
3
IndexKeysDefinition<BravoPoint> keys = "{ Location: \"2dsphere\" }";
var indexModel = new CreateIndexModel<BravoPoint>(keys);
var s = collection.Indexes.CreateOne(indexModel);

最後可以得出Result

1
2
3
4
var gp = new GeoJsonPoint<GeoJson2DGeographicCoordinates>
(new GeoJson2DGeographicCoordinates(121.575163, 25.078124));
var query = Builders<BravoPoint>.Filter.Near("Location", gp, 10000);
var result = collection.Find(query).ToList();

MongoDb

MongoDB 學習筆記四 C#調用MongoDB