我有班级推销员
public class Salesman
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[JsonConverter(typeof(ObjectIdConverter))]
public ObjectId Id { get; set; }
[BsonElement("complementTime")]
public DateTime ComplementTime { get; set; }
[BsonElement("futureWorker")]
public FutureWorkerInfo FutureWorker { get; set; }
}
和班级顾问
public class Consultant
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[JsonConverter(typeof(ObjectIdConverter))]
public ObjectId Id { get; set; }
[BsonElement("complementTime")]
public DateTime ComplementTime { get; set; }
[BsonElement("futureWorker")]
public FutureWorkerInfo FutureWorker { get; set; }
[BsonElement("recommendedWorkers")]
public FutureWorkerInfo[] RecommendedWorkers { get; set; }
}
这两个类都有类
public class FutureWorkerInfo
{
[BsonElement("name")]
public string Name { get; set; }
[BsonElement("homeAddress")]
public FutureWorkerHomeAddress HomeAddress { get; set; }
[BsonElement("phone")]
public string Phone { get; set; }
[BsonElement("secondPhone")]
public string SecondPhone { get; set; }
[BsonElement("email")]
public string Email { get; set; }
[BsonElement("birthDate")]
public string BirthDate { get; set; }
}
我需要忽略字段" homeAddress"当我使用课程"推销员"。这意味着我不必仅在Salesman中插入或读取mongo数据库这个字段。当我使用顾问我应该'可以'一些操作与字段" homeAddress"。
答案 0 :(得分:0)
您可以使用界面执行此操作。
创建ISalesmanFutureWorkInfo
public interface ISalesmanFutureWorkInfo
{
string Name { get; set; }
string Phone { get; set; }
string SecondPhone { get; set; }
string Email { get; set; }
string BirthDate { get; set; }
}
创建一个具有HomeAddress的IConsultantFutureWorkInfo并继承ISalesmanFutureWorkInfo
public interface IConsultantFutureWorkInfo : ISalesmanFutureWorkInfo
{
FutureWorkerHomeAddress HomeAddress { get; set; }
}
更改类以使用接口。
public class Consultant
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[JsonConverter(typeof(ObjectIdConverter))]
public ObjectId Id { get; set; }
[BsonElement("complementTime")]
public DateTime ComplementTime { get; set; }
[BsonElement("futureWorker")]
public IConsultantFutureWorkInfo FutureWorker { get; set; }
[BsonElement("recommendedWorkers")]
public IConsultantFutureWorkInfo[] RecommendedWorkers { get; set; }
}
public class Salesman
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
[JsonConverter(typeof(ObjectIdConverter))]
public ObjectId Id { get; set; }
[BsonElement("complementTime")]
public DateTime ComplementTime { get; set; }
[BsonElement("futureWorker")]
public ISalesmanFutureWorkInfo FutureWorker { get; set; }
}
答案 1 :(得分:0)
[BsonElement("homeAddress")]
[BsonIgnoreIfNull]
public FutureWorkerHomeAddress HomeAddress { get; set; }
现在一切都还好