Servicestack: Handle indexes, auto increment etc without attributes?

旧街凉风 提交于 2019-12-11 04:55:16

问题


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; }

However, OrmLite/ServiceStack states that:

  • Map a POCO class 1:1 to an RDBMS table, cleanly by conventions, without any attributes required.

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.

Is this doable?


EDIT:

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


回答1:


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.

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 });

Which will have the same effect as decorating your property with [Index(Unique = true)]



来源:https://stackoverflow.com/questions/19884733/servicestack-handle-indexes-auto-increment-etc-without-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!