【问题标题】:UWP EF SQLite referencing to inherited types throws exception引用继承类型的 UWP EF SQLite 引发异常
【发布时间】:2016-03-13 12:02:55
【问题描述】:

我们希望在 Completion 中引用继承的 Control,因为一个 Control 具有可能的 Completion 列表并保存选定的 Completion。 我们的桌子是

public class Completion
{
    public int CompletionId { get; set; }
    public string Text { get; set; }

    public virtual Control Control { get; set; }
}
public class Control : BaseControl
{
    public int ControlId { get; set; }

    public virtual Completion Completion { get; set; }

    public virtual ICollection<Completion> Completions { get; set; }
}

这些表的迁移是可行的,但是如果我们尝试向数据库添加 Completion,则会出现异常

{"SQLite 错误 1: '外键不匹配 - \"Completion\" 引用 \"Control\"'"}

我们添加这样的 Completion

var completion = new Completion { Text = "completion1" }; db.Completions.Add(completion); db.SaveChanges();

【问题讨论】:

    标签: c# entity-framework sqlite uwp


    【解决方案1】:

    从错误信息来看,你的问题是关于 SQLite 而不是 EF。

    Completion 类上的 public virtual Control Control { get; set; } 被视为您未定义的控制表的外键。

    我的猜测是您不想将 Control 引用保存到 SQLite 中。所以在这种情况下,让 SQLite 忽略该列

    [SQLite.Ignore]
    public virtual Control Control { get; set; }
    

    【讨论】:

    • 但是我们定义了一个控制表(我们有控制和完成),我们想把控制保存在一个完成中。一个控件有一个可能的 Completions 列表和一个特定 Completions 是选定的一个
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2013-01-14
    • 2013-08-28
    • 2015-07-18
    相关资源
    最近更新 更多