0

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

Community
  • 1
  • 1
Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89

2 Answers2

0

Is your DB still there? This can happend if database is deleted. If it is not deleted, then try delete the migration script, Add-Migration (via NuGet) and update your database.

Yuniku_123
  • 269
  • 1
  • 5
  • 18
  • Yes, the database is still there... I have even tried renaming all the "id" fields to "SicknessId" and "SyptomId" and get the same error – Trevor Daniel Mar 30 '16 at 12:52
  • I updateded my comment. Please, check it. This works for me. – Yuniku_123 Mar 30 '16 at 12:53
  • That makes no sense. If I delete the migration script then there is nothing for the "update-database" to execute? – Trevor Daniel Mar 30 '16 at 12:55
  • I get that error output when I used Code First approach and have existing Migration scripts and are trying to overwrite a change (i.e. renaming a column) that has since been deleted. After I did the above, the error has gone. – Yuniku_123 Mar 30 '16 at 12:58
  • run in the package manager console : update-database -verbose -sourcemigration $initialdatabase to see what is causing the migration to fail. – Yuniku_123 Mar 30 '16 at 13:56
0

I think your problem is in the relationship...how do you have configured this tables? you could add a virtual class to the relationship between this classes like a navigation property

public class UserSickness {
    public int id { get; set; }
    public DateTime SicknessDate { get; set; }
    public List<Symptom> Symptoms { get; set; }

    // Navigation property
    public virtual User UserAsociated { get; set; }
}

public class Symptom {
    public int id { get; set; }
    public string Description { get; set; }

    // Navigation property
    public virtual UserSickness UserSicknessAsociated { get; set; }
}

Then, change the configuration to

protected override void OnModelCreating(DbModelBuilder modelBuilder) {

// delete symptoms
modelBuilder.Entity<DatapultData.Model.UserSickness>()
.HasOptional(a => a.Symptoms)
.WithRequired(b => b.UserSicknessAsociated)//Update this line
.WillCascadeOnDelete(true);

base.OnModelCreating(modelBuilder);
}

And for avoid similar errors, you should make the same for the User class.

Maybe this could solve the ambiguos column names, showed in the error message