本文介绍了db4o,Linq和UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,db4o网站最近已重做,现在旧的URL出现404错误.每当我认为找到答案时,都会收到404错误.

Apparently the db4o website was recently redone, and now old urls are giving 404 errors. Everytime I think I've found the answer, I get a 404 error.

我有一个简单的db4o数据库,用于存储人员.

I have a simple db4o database I've setup to store people.

public class Person
{
     public string Firstname { get; set;}
     public string Lastname {get;set;}
}

我已经能够在数据库上运行Linq查询,并且一切正常.我在Web环境中进行编程,因此需要有人来识别数据库中的唯一对象并从中获取唯一的对象,因此我只需将数据库配置为使用UUID.现在问题就变成了,如何从Linq查询中获取的对象获取UUID信息?

I've been able to run Linq queries on the database, and everything works wonderfully. I'm programming in a web environment, so I will need someone to identify and fetch unique objects from the database, so I simply configured the database to use UUID's. Now the problem has become, how can I get to the UUID information from the objects I get from the Linq query?

例如,假设我将所有人员存储在数据库中,并且需要为每个人生成唯一的URL.我将使用UUID来做到这一点.因此,我将运行它:

For example, let's say I get all the people stored in the database, and I need to generate the unique URL's for each person. I will use the UUID to do so. So I'll run this:

var people = (from Person p in db select p).ToList();

然后我将遍历列表

foreach( Person person in people)
{
  DoSomething(person);
}

最好的解决方案是将UUID设置为Person类的一个属性,这样我就可以:

The best solution would be making the UUID a property on the Person class, so that I could just:

var uuid = person.UUID;

但是,我看不到这样做的机制.我想念什么吗?

I don't see a mechanism for doing that however. Am I missing something?

推荐答案

我已经回答了这个问题,但还有其他问题.因此,我再次回答:在db4o中,通常没有ID. db4o使用对象身份区分对象.因此,内存中的同一对象将成为数据库的同一对象.

I've answered this already here but together with other question. Therefore I answer it again:In db4o you have normally no id. db4o uses the object-identity to distinguish the object apart. So the same object in memory is going to be the same object for the database.

只要您不序列化对象就可以了,就不需要任何ID.但是,一旦对象被序列化/断开连接,它就不再起作用了(对于Web应用程序是典型的).

As long a you don't serialize object this works fine, you don't need any ID. However as soon as objects are serialized / disconnected this doesn't work anymore (Typical for web-apps).

我认为这三个选项是可能的:

I think this three options are possible:

  • 使用db4o 内部ID .您可以使用IObjectContainer.Ext().GetID(object)获取此ID.当然,您可以通过id IObjectContainer.Ext().GetByID(id)获取对象.但是,此ID不会永远存在.对数据库进行碎片整理会更改此ID.
  • 使用db4o的 UUID .但是db4o UUID很大
  • 自己创建ID.例如,使用GUID.或者使用聪明的ID生成器.当您在查询中使用它.
  • Use the db4o internal id. You can get this id with IObjectContainer.Ext().GetID(object). And of course you can get a object by id: IObjectContainer.Ext().GetByID(id). However this id isn't forever. Defragmenting the database changes this id.
  • Using db4o's UUIDs. But db4o UUIDs are quite large
  • Creating ids by yourself. For example using a GUID. Or using a clever id-generator. Remember to index such a id-field when you use it in queries.

当然,您可以创建一个基类,该基类提供上述任何实现的id属性.

Of course you can create a base-class which provides the id-property with any of the implementations above.

这篇关于db4o,Linq和UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:34