Entity framework parent -> child linking and foreign key constrain failed error
<p>I'm using entity framework 7 (core) and Sqlite database. Currently using comboBox to change entity category with this method:</p>

<pre><code>/// &lt;summary&gt;
/// Changes the given device gategory.
/// &lt;/summary&gt;
/// &lt;param name="device"&gt;&lt;/param&gt;
/// &lt;param name="category"&gt;&lt;/param&gt;
public bool ChangeCategory(Device device, Category category)
{
    if (device != null &amp;&amp; category != null )
    {
        try
        {
            var selectedCategory = FxContext.Categories.SingleOrDefault(s =&gt; s.Name == category.Name);
            if (selectedCategory == null) return false;
            if (device.Category1 == selectedCategory) return true;

            device.Category1 = selectedCategory;
            device.Category = selectedCategory.Name;
            device.TimeCreated = DateTime.Now;
            return true;
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Category change for device failed. Possible reason: database has multiple categories with same name.");
        }
    }
    return false;
}
</code></pre>

<p>This function changes the category Id for the <code>device</code> just fine. But is this correct way?</p>

<p>After linking and then later on while deleting this <code>category</code> I get an error from the Sqlite database:</p>

<blockquote>
  <p>{"SQLite Error 19: 'FOREIGN KEY constraint failed'"}</p>
</blockquote>

<p><strong>The delete category method</strong></p>

<pre><code>public bool RemoveCategory(Category category)
{
    if (category == null) return false;
    var itemForDeletion = FxContext.Categories
        .Where(d =&gt; d.CategoryId == category.CategoryId);
    FxContext.Categories.RemoveRange(itemForDeletion);
    return true;
}
</code></pre>

<p><strong>EDIT</strong>
Here are the structures of <code>device</code> and <code>category</code>:</p>

<pre><code>CREATE TABLE "Category" (
    "CategoryId" INTEGER NOT NULL CONSTRAINT "PK_Category" PRIMARY KEY AUTOINCREMENT,
    "Description" TEXT,
    "HasErrors" INTEGER NOT NULL,
    "IsValid" INTEGER NOT NULL,
    "Name" TEXT
)

CREATE TABLE "Device" (
    "DeviceId" INTEGER NOT NULL CONSTRAINT "PK_Device" PRIMARY KEY AUTOINCREMENT,
    "Category" TEXT,
    "Category1CategoryId" INTEGER,
    CONSTRAINT "FK_Device_Category_Category1CategoryId" FOREIGN KEY ("Category1CategoryId") REFERENCES "Category" ("CategoryId") ON DELETE RESTRICT,
)
</code></pre>