ormlite 中的自定义引用命名约定

Custom reference naming convention in ormlite

有没有办法更改引用和外键的默认 {PropertyReference}Id 命名约定?

例如,我想这样做:

public class Customer
{
    [References(typeof(CustomerAddress))]
    public int Id_PrimaryAddress { get; set; } // with a prefix

    [Reference]
    public CustomerAddress PrimaryAddress { get; set; }
}

而不是:

public class Customer
{
    [References(typeof(CustomerAddress))]
    public int PrimaryAddressId { get; set; } // standard

    [Reference]
    public CustomerAddress PrimaryAddress { get; set; }
}

谢谢

您无法全局更改 OrmLite's Reference Conventions 的代码约定,但您可以使用 [Alias("DbColumnName")] 将其映射到不同的底层 RDBMS Table 列。

使用属性覆盖约定

您还可以使用 Foreign Key and References Attributes as your example does to override the conventions, e.g. you can play with this Live Example on Gistlyn:

public class CustomerAddress 
{
    [AutoIncrement]
    public int Id { get; set; }
    public string Address { get; set; }
    public string Country { get; set; }
}

public class Customer
{
    [AutoIncrement]
    public int Id { get; set; }
    public string Name { get; set; }

    [References(typeof(CustomerAddress))]
    public int Id_PrimaryAddress { get; set; } // with a prefix

    [Reference]
    public CustomerAddress PrimaryAddress { get; set; }
}

db.CreateTable<Customer>();
db.CreateTable<CustomerAddress>();

var customer = new Customer
{
    Name = "The Customer",
    PrimaryAddress = new CustomerAddress {
        Address = "1 Home Street",
        Country = "US"
    },
};

db.Save(customer, references:true);

您可以在哪里加载它及其引用并通过以下方式查看它:

var c = db.LoadSelect<Customer>(x => x.Name == "The Customer");

c.PrintDump();

将输出:

[
    {
        Id: 1,
        Name: The Customer,
        Id_PrimaryAddress: 1,
        PrimaryAddress: 
        {
            Id: 1,
            Address: 1 Home Street,
            Country: US
        }
    }
]