问题
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