【问题标题】:How to write a simple OneToOne relationship?如何写一个简单的 OneToOne 关系?
【发布时间】:2016-03-15 13:25:36
【问题描述】:

我想在我的模型之间写一个简单的一对一关系。 我从sqlite-net 收到的异常消息是Don't know about Profile

public class User
{
    [PrimaryKey]
    public int pk { get; set; }

    public string first_name { get; set;}

    [OneToOne]
    public Profile profile { get; set;}

     ...
}

public class Profile
{
    [PrimaryKey]
    public int pk { get; set; }

    [ForeignKey(typeof(User))]
    public string UserID { get; set; }

    [OneToOne]
    public User user { get; set;}
}

我在这里缺少什么? 谢谢

【问题讨论】:

  • 当您的用户 pk 是 int 时,为什么 UserId 是 string
  • 没什么区别。

标签: c# sqlite-net


【解决方案1】:

您可以看到源代码here,该异常在第 2044 行被抛出。如果您检查该函数,您会看到它正在检查是否支持 C# 类型,然后返回相关的 SQL 类型。

不支持您尝试添加的 Profile 类型,因为它是您创建的自定义类型,因此 sqlite-net 无法知道为其返回什么 SQL 类型。

尝试重新设计您的代码,以便仅在受支持的类型上需要 OneToOne 关系。

【讨论】:

  • 谢谢!我不能让sqlite自动创建关系表吗?
  • 恐怕我不是这方面的专家,但从答案here 看来,可能有一种方法可以满足您的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多