简体   繁体   English

实体框架代码优先-级联删除

[英]Entity Framework Code First - Cascading Delete

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." “参数@objname模棱两可,或者声明的@objtype(COLUMN)错误。”

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: 我遵循以下SO问题:

Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship 一对一或一对一关系的实体框架(EF)代码优先级联删除

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 谢谢

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. 如果未删除,请尝试删除迁移脚本,添加迁移(通过NuGet)并更新数据库。

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. 为了避免类似的错误,您应该对User类进行相同的设置。

Maybe this could solve the ambiguos column names, showed in the error message 也许这可以解决错误消息中显示的歧义列名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM