Entity Framework Code First.
I am trying to implement a cascading delete on a table but getting the error
"Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong."
I have the following model:
A Users Table:
public class User {
public int id { get; set; }
public List<UserSickness> Sickness { get; set; }
}
A Sickness Table:
public class UserSickness {
public int id { get; set; }
public DateTime SicknessDate { get; set; }
public List<Symptom> Symptoms { get; set; }
}
A Symptoms Table:
public class Symptom {
public int id { get; set; }
public string Description { get; set; }
}
So, basically, I want a user to have a "Sick" day and be able to say what his/her symptoms were...
Equally, and this is what I am having problems implementing. When I delete a "sickness" for a particular date I want a cascading delete to remove the "symptoms"
I have followed the following SO question:
Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship
and added the following code:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
// delete symptoms
modelBuilder.Entity<DatapultData.Model.UserSickness>()
.HasOptional(a => a.Symptoms)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
When I add a migration to implement this it generates the following code:
namespace Data.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class addedcascadingdeleteonsymptoms : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.Symptoms", "UserSickness_id", "dbo.UserSicknesses");
DropIndex("dbo.Symptoms", new[] { "UserSickness_id" });
RenameColumn(table: "dbo.UserSicknesses", name: "UserSickness_id", newName: "Symptoms_id");
CreateIndex("dbo.UserSicknesses", "Symptoms_id");
AddForeignKey("dbo.UserSicknesses", "Symptoms_id", "dbo.Symptoms", "id", cascadeDelete: true);
DropColumn("dbo.Symptoms", "UserSickness_id");
}
public override void Down()
{
AddColumn("dbo.Symptoms", "UserSickness_id", c => c.Int());
DropForeignKey("dbo.UserSicknesses", "Symptoms_id", "dbo.Symptoms");
DropIndex("dbo.UserSicknesses", new[] { "Symptoms_id" });
RenameColumn(table: "dbo.UserSicknesses", name: "Symptoms_id", newName: "UserSickness_id");
CreateIndex("dbo.Symptoms", "UserSickness_id");
AddForeignKey("dbo.Symptoms", "UserSickness_id", "dbo.UserSicknesses", "id");
}
}
}
But, when I do a "update-database" I get the following error which I don't understand how to fix..
Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
Can anyone suggest what I am doing wrong?
Thanks