等效于.WillCascadeOnDelete(false);的实体框架数据注释 [英] Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

查看:92
本文介绍了等效于.WillCascadeOnDelete(false);的实体框架数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前使用的是启用了迁移功能的EF Code First 4.3,但是禁用了自动迁移功能。

I'm currently using EF Code First 4.3 with migrations enabled, but automatic migrations disabled.

我的问题很简单,是否有与模型等效的数据注释配置。WillCascadeOnDelete(false)

My question is simple, is there a data annotations equivalent of the model configuration .WillCascadeOnDelete(false)

我想装饰我的课程,以便外键关系不会触发级联删除。

I would like to decorate my class so that the foreign key relationships do NOT trigger a cascading delete.

代码示例:

public class Container
{
    public int ContainerID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Output> Outputs { get; set; }
}

public class Output
{
    public int ContainerID { get; set; }
    public virtual Container Container { get; set; }

    public int OutputTypeID { get; set; }
    public virtual OutputType OutputType { get; set; }

    public int Quantity { get; set; }
}  

public class OutputType 
{
    public int OutputTypeID { get; set; }
    public string Name { get; set; }
}

我想做这样的事情:

public class Output
{
    [CascadeOnDelete(false)]   
    public int ContainerID { get; set; }
    public virtual Container Container { get; set; }

    [CascadeOnDelete(false)]    
    public int OutputTypeID { get; set; }
    public virtual OutputType OutputType { get; set; }

    public int Quantity { get; set; }
}  

这样,我就能正确地支持迁移。

This way i would be able to scaffold the migration correctly. which scaffolds the foreign key relationships to be cascade deleted at the moment.

除了使用模型配置之外,还有其他想法吗?

Any ideas, other than using Model Configuration?

推荐答案

没有这样的等效项。您必须使用fluent API选择性删除级联删除,或者必须删除 OneToManyCascadeDelete 约定以将其全局删除。

No there is no such equivalent. You must use fluent API to remove cascade delete selectively or you must remove OneToManyCascadeDelete convention to remove it globally.

这篇关于等效于.WillCascadeOnDelete(false);的实体框架数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆