可以使用数据注释来执行使用实体框架4.1 RC的级联删除吗? [英] Can I use Data Annotations to perform a Cascade Delete with Entity Framework 4.1 RC?

查看:141
本文介绍了可以使用数据注释来执行使用实体框架4.1 RC的级联删除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用EF4.1 RC的数据注释时,是否有注释导致级联删除?

  public class Category 
{
public int Id {get;组;
[必需]
public string Name {get;组; }
public ICollection< Product>产品{get;组; }
}

public class Product
{
public int Id {get;组; }
public string Name {get;组; }
public Category Category {get;组; }
}

使用此模型生成的约束是:

  ALTER TABLE [Product] ADD CONSTRAINT [Product_Category] ​​
FOREIGN KEY([Category_Id])参考[Categorys]([Id])
更新无更新行动;

如果不是如何实现?

解决方案

在产品表上放置所需的类别关系字段解决这个

  public class Product 
{
public int Id {get;组; }
public string Name {get;组; }

[必需] //< =======强制级联删除
public Category Category {get;组; }
}


When using data annotations with EF4.1 RC is there an annotation to cause cascade deletes?

public class Category
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public ICollection<Product> Products { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Category Category { get; set; }
}

Using this model the constraint generated is:

ALTER TABLE [Product] ADD CONSTRAINT [Product_Category] 
FOREIGN KEY ([Category_Id]) REFERENCES [Categorys]([Id]) 
ON DELETE NO ACTION ON UPDATE NO ACTION;

If not how is it achieved?

解决方案

Putting required on the Product table Category relationship field solves this

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    [Required]  //<======= Forces Cascade delete
    public Category Category { get; set; }
}

这篇关于可以使用数据注释来执行使用实体框架4.1 RC的级联删除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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