【问题标题】:Custom reference naming convention in ormliteormlite 中的自定义引用命名约定
【发布时间】:2019-03-30 08:50:41
【问题描述】:

有没有办法更改引用和外键的默认 {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; }
}

谢谢

【问题讨论】:

    标签: servicestack ormlite-servicestack


    【解决方案1】:

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

    用属性覆盖约定

    您还可以像您的示例那样使用Foreign Key and References Attributes 来覆盖约定,例如你可以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
            }
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 2011-04-03
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 2011-04-26
      相关资源
      最近更新 更多