简体   繁体   English

NET的MongoDb创建POCO ID值

[英]MongoDb with .net creating POCOs id values

I am new to MongoDb but have an existing application which would benefit heavily from using it to store encapsulated data for fast retrieval and updating. 我是MongoDb的新手,但是拥有一个现有应用程序,可以使用它来存储封装的数据以进行快速检索和更新,从而从中受益匪浅。

Now the problem I have is that I have existing models which are just simple POCOs without any framework specific attributes or dependencies, they do however have an Id field which is an int (some differ but its easier to just assume all are ints in this instance). 现在的问题是,我有现有的模型,它们只是简单的POCO,没有任何框架特定的属性或依赖关系,但是它们确实有一个ID字段,它是一个int型(有些不同,但在这种情况下更容易假设所有都是int型) )。 Now without tying that object to MongoDb (be it attribute round the id field or changing their types to ObjectId fields) is there any way for it to automagically pick up the id field through external config? 现在,无需将该对象绑定到MongoDb(它是围绕id字段属性还是将其类型更改为ObjectId字段),是否可以通过外部配置自动获取id字段?

If not its not the end of the world I will just make a domain object that is mapped to and from for MongoDb purposes like I have with Nhibernate. 如果不是世界末日,那么我将像与Nhibernate一样,创建一个用于MongoDb来往的域对象。 I would just rather not have to have 2 versions of the same classes just differing with the Id field. 我只希望不必具有与Id字段不同的相同类的2个版本。 As in this case the domains will be identical. 在这种情况下,域将是相同的。

The C# driver's support for serialization is described here: C#驱动程序对序列化的支持描述如下:

http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial

A principal of the serialization support is that while annotating your domain objects with attributes is by far the easiest way to control serialization, anything you can do with attributes can also be done without them for those developers who want to keep their domain model persistence ignorant and free of dependencies on MongoDB. 序列化支持的一个原则是,尽管用属性注释域对象是控制序列化的最简单方法,但是对于那些希望保持其域模型持久性不了解的开发人员,无需属性也可以完成任何操作不依赖MongoDB。

Look for examples using RegisterClassMap in the tutorial. 在教程中使用RegisterClassMap查找示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM