使用 ServiceStack ORMLite 保存引用
saving reference using ServiceStack ORMLite
我正在使用 ORMLite 作为我的 ORM,我将它与包含外键关系的以下结构一起使用:
public class Order
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Item Item { get; set; }
public string ProUserId { get; set; }
public string Details { get; set; }
}
public class Item
{
[AutoIncrement]
public int Id { get; set; }
public string Description { get; set; }
}
正如我们所见,Order 包含对 Item 的引用。在数据库订单中 table 在 table 中有一个名为 ItemId 的外键,我在设计视图中使用 [Reference] 属性对该键进行了注释。
我正在尝试使用以下代码保存订单:
var order = new Order
{
Item = new Item
{
Id = 3,
Description = "Something"
},
ProUserId = "kunal@kunal.com",
Details = "fdfsdfsd"
};
Db.Save(order,references:true);
我希望 ORMLite 会在订单中找到关系并与 ItemID table 但它没有,而是抛出以下错误:
Cannot insert the value NULL into column 'ItemId', table 'WebApp.dbo.Order'; column does not allow nulls. INSERT fails.
我尝试更改我的架构,并在我的项目 table 中添加了 OrderId 列并在其中引用,并且效果很好。但这不是正确的设计。我是否应该对 code/schema 进行任何更改以支持此功能?
您仍然需要提供 OrmLite 可以用来存储关系的外键,例如在 Child/ForeignKey table:
public class Order
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Item Item { get; set; }
public string ProUserId { get; set; }
public string Details { get; set; }
}
public class Item
{
[AutoIncrement]
public int Id { get; set; }
public int OrderId { get; set; } //Parent Table PK
public string Description { get; set; }
}
或者对于 1:1 关系,可以在父 table 上,例如:
public class Order
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Item Item { get; set; }
public int ItemId { get; set; } //Child Table PK
public string ProUserId { get; set; }
public string Details { get; set; }
}
public class Item
{
[AutoIncrement]
public int Id { get; set; }
public string Description { get; set; }
}
我正在使用 ORMLite 作为我的 ORM,我将它与包含外键关系的以下结构一起使用:
public class Order
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Item Item { get; set; }
public string ProUserId { get; set; }
public string Details { get; set; }
}
public class Item
{
[AutoIncrement]
public int Id { get; set; }
public string Description { get; set; }
}
正如我们所见,Order 包含对 Item 的引用。在数据库订单中 table 在 table 中有一个名为 ItemId 的外键,我在设计视图中使用 [Reference] 属性对该键进行了注释。
我正在尝试使用以下代码保存订单:
var order = new Order
{
Item = new Item
{
Id = 3,
Description = "Something"
},
ProUserId = "kunal@kunal.com",
Details = "fdfsdfsd"
};
Db.Save(order,references:true);
我希望 ORMLite 会在订单中找到关系并与 ItemID table 但它没有,而是抛出以下错误:
Cannot insert the value NULL into column 'ItemId', table 'WebApp.dbo.Order'; column does not allow nulls. INSERT fails.
我尝试更改我的架构,并在我的项目 table 中添加了 OrderId 列并在其中引用,并且效果很好。但这不是正确的设计。我是否应该对 code/schema 进行任何更改以支持此功能?
您仍然需要提供 OrmLite 可以用来存储关系的外键,例如在 Child/ForeignKey table:
public class Order
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Item Item { get; set; }
public string ProUserId { get; set; }
public string Details { get; set; }
}
public class Item
{
[AutoIncrement]
public int Id { get; set; }
public int OrderId { get; set; } //Parent Table PK
public string Description { get; set; }
}
或者对于 1:1 关系,可以在父 table 上,例如:
public class Order
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public Item Item { get; set; }
public int ItemId { get; set; } //Child Table PK
public string ProUserId { get; set; }
public string Details { get; set; }
}
public class Item
{
[AutoIncrement]
public int Id { get; set; }
public string Description { get; set; }
}