Servicestack:处理没有属性的索引、自动增量等? [英] Servicestack: Handle indexes, auto increment etc without attributes?

查看:38
本文介绍了Servicestack:处理没有属性的索引、自动增量等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试 OrmLite,我正在研究如何处理表中的索引

I am testing OrmLite, and I am looking at how to handle indexes in the tables that are created.

如果您想将某物标记为索引、唯一性、自动增量等,我发现的唯一方法是通过属性,如下所示:

The only way that I have found if you want to mark something as an index, unique, auto_increment etc is via attributes, like this:

Index(Unique = true)] // Creates Unique Index
public string Email { get; set; }

然而,OrmLite/ServiceStack 声明:

However, OrmLite/ServiceStack states that:

  • 按照约定将 POCO 类 1:1 映射到 RDBMS 表,无需任何属性.

因此我希望有其他方法可以在不使用属性的情况下定义这些东西?包含类定义的库应该与 OrmLite 完全分离.

And I was thus hoping that there are other ways of defining these things without using attributes? The library with the class definitions should be completely separated from OrmLite.

这可行吗?

由于某种原因,扩展方法 AddAttributes 似乎不起作用:

The extension method AddAttributes does not seem to function for some reason:

推荐答案

按照惯例,意味着 OrmLite 将按照预期从模型中推断出模式.但是,如果您想添加任何自定义项,例如在任意字段上添加索引,则您确实需要告诉 OrmLite.由于 OrmLite 是代码优先的 ORM,属性是如何装饰模型的附加功能.

By convention, means that OrmLite will infer the schema from the model as can be expected. But if you want to add any customizations like adding an index on arbitrary fields, than you do need to tell OrmLite about them. As OrmLite is a code-first ORM, attributes are how to decorate additional functionality to your models.

在下一个 OrmLite v4 中,您将能够通过在启动时动态添加这些与 POCO 分离的属性,例如:

In the next OrmLite v4 you will be able to add these attributes decoupled from your POCO by adding them dynamically at startup, e.g:

typeof(Poco).GetProperty("Email")
    .AddAttributes(new IndexAttribute { Unique = true });

这与使用 [Index(Unique = true)]

这篇关于Servicestack:处理没有属性的索引、自动增量等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆