'Id' 요소가 클래스의 어떤 필드나 속성과도 일치하지 않습니다.
MongoDB에 있는 컬렉션에서 결과를 얻었는데, 구조는 아래와 같습니다.
[DataContract]
public class Father
{
[BsonId]
[DataMember]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public List<Child> childs { get; set; }
}
[DataContract]
public class Child
{
[DataMember]
public string Id { get; set; }
[DataMember]
public int Name { get; set; }
}
시도할 때:
List<Father> f = result.ToList();
이트 콜Element 'Id' does not match any field or property of the class Model.Child
그냥 'Id'를 다른 것으로 받아들이는 것 같아요.
어떻게 하면 좋을까요?감사해요.
다음을 추가하여 문제를 해결할 수 있습니다.[BsonIgnoreExtraElements]
학급 선언서의 맨 위에 ObjectId
는 MongoDB에 의해 내부적으로 유지 관리되며 개체에서 타임스탬프와 같은 추가 정보를 가져오지 않는 한 필요하지 않을 수 있습니다.이게 도움이 되길 바랍니다.
var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) };
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
이것은 완벽하게 작동합니다![BsonIgnoreExtraElements]도 잘 작동했지만, CamelCaseElementNameConvention과 같은 다른 ConventionRegistry를 추가하려는 경우 속성 1을 재정의하고 동일한 예외가 발생합니다.다른 속성을 사용하여 이 작업을 수행할 수 있을지 확신할 수 없습니다.
동적 목록(List)을 사용하여 필터를 빌드하고 있었는데 비슷한 오류가 발생했습니다.문제를 해결하기 위해 데이터 클래스에 이 행을 추가했습니다.
[BsonId]
public ObjectId Id { get; set; }
나의 경우를 위한 이 작업: 속성 추가.
[DataMember]
[BsonElement("songName")]
요소 위에:
[BsonIgnoreExtraElements]
public class Music
{
[BsonId]
[DataMember]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
[BsonElement("songName")]
public string SongName { get; set; }
[DataMember]
[BsonElement("artistName")]
public string ArtistName { get; set; }}
저도 같은 문제에 직면했습니다.동일한 오류가 발생했습니다.var data = query.ToList();
var collection = db.GetCollection<Product>("Products");
var query =
from e in collection.AsQueryable<Product>()
where e.name == "kalem"
select e;
var data = query.ToList();
제 삽입물은 다음과 같습니다.
var collection = db.GetCollection<Product>("Products");
collection.InsertBatch(products);
저는 아래와 같이 해결했습니다.
ObjectId id = new ObjectId();
var collection = db.GetCollection<Product>("Products");
collection.InsertBatch(products);
id = pr.Id;
그리고 나는 덧붙였습니다.id
아래 제품 클래스와 같은 제품 클래스로
class Product
{
public ObjectId Id { get; set; }
public string name { get; set; }
public string category { get; set; }
public double price { get; set; }
public DateTime enterTime { get; set; }
}
BsonNoId 속성을 사용할 수 있습니다.
[DataContract]
[BsonNoId]
public class Child
{
[DataMember]
public string Id { get; set; }
[DataMember]
public int Name { get; set; }
}
클래스의 맨 위에 이것을 추가하십시오 [BsonIgnoreExtraElements].
ObjectId 속성의 [DataMember]을(를) 제거하고 ID를 ObjectId_id에 바인딩하기만 하면 됩니다.
그래서 당신의 수업은 다음과 같이 되어야 합니다:
[DataContract]
public class Father
{
[BsonId]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id {
get { return _id.ToString(); }
set { _id = ObjectId.Parse(value); }
}
[DataMember]
public List<Child> childs { get; set; }
}
ps : 당신의 경우, 하위 ID를 수동으로 생성해야 합니다. 만약 당신이 그것을 objectId(mongo)로 하고 싶다면, 당신은 마침내 동일한 트릭을 할 것입니다. 객체를 역직렬화하기 위해서, 당신은 뉴턴소프트.json 참조를 사용하고 이렇게 해야 합니다.
Father = JsonConvert.DeserializeObject<Father>(response.Content);
자식 클래스는 아버지 상속을 지정해야 합니다.
공용 클래스 자녀: 아버지 {...}
Father 클래스는 WCF에 대해 알려진 유형 특성을 추가해야 합니다.
[DataContract]
[KnownType(typeof(Child))]
public class Father
저장/가져오는 MongoCollection("아버지")인 경우 예상되는 각 자식 유형에 대한 클래스 맵을 등록해야 할 수 있습니다.
if (!BsonClassMap.IsClassMapRegistered(typeof(Child)))
{
BsonClassMap.RegisterClassMap<Child>(
cm => { cm.AutoMap(); });
}
@alexjamesbrown이 언급했듯이, 당신은 당신의 poco 객체의 id 필드의 이름을 '_id'로 지정할 필요가 없습니다.상속이 있는 아이디어는 상속입니다.그러므로 아버지의 "id" 필드(이름이 무엇이든)를 사용하는 것으로 충분합니다.왜 당신의 아버지 클래스가 Id와 _id 속성을 모두 가지고 있는지는 분명하지 않습니다.그 중 하나는 아마도 필요하지 않을 것입니다.
언급URL : https://stackoverflow.com/questions/11557912/element-id-does-not-match-any-field-or-property-of-class
'programing' 카테고리의 다른 글
MongoDB 셸에서 실행 중인 쿼리를 중단하려면 어떻게 해야 합니까? (0) | 2023.05.04 |
---|---|
Angular를 사용하는 이유JS 선택에 빈 옵션이 포함되어 있습니까? (0) | 2023.05.04 |
NPM: npm을 실행할 때 npm-cli.js를 찾을 수 없습니다. (0) | 2023.05.04 |
서로 다른 두 분기의 파일을 비교하는 방법 (0) | 2023.05.04 |
에서 응용 프로그램의 경로를 가져오려면 어떻게 해야 합니까?NET 콘솔 애플리케이션? (0) | 2023.05.04 |