问题描述
我与外键有一对一的关系,但由于某些原因, Cascade Delete
未启用。示例代码如下。
public class AppRegistration
{
public int AppRegistrationId {get;组;
[必需]
[StringLength(50)]
[Display(Name =Username)]
public string UserName {get;组;
[必需]
[StringLength(100)]
public string Password {get;组; }
[StringLength(20)]
public string StudentOrAgent {get;组; }
//导航属性
public virtual AppStatus AppStatus {get;组; }
公共虚拟协议协议{get;组; }
public virtual AnotherTable AnotherTable {get;组; }
}
具有外键的从属表位于下面。
public class协议
{
[Key]
[ForeignKey(AppRegistration)]
public int AppRegistrationId {get;组; }
public DateTime DateAgreed {get;组; }
public virtual AppRegistration AppRegistration {get;组; }
}
当我尝试从生成的 AppRegistrations
table我得到一个引用约束冲突。
我尝试在依赖表格的导航属性中放入 [必需]
,但不执行任何操作 - Update-Database
命令显示不等待基于代码的迁移。
消息。有任何想法吗?谢谢。
更新:
我收到以下错误消息:
DELETE语句与REFERENCE约束FK_dbo.AppStatus_dbo.AppRegistrations_AppRegistrationId冲突。冲突发生在数据库MVCapp,表dbo.AppStatus,AppRegistrationId列。
我决定在单独的示例项目中制定级联删除
问题。我发现以下博客MSDN页面非常有用。
-
I have one to one relationship with foreign keys but the
Cascade Delete
is not enabled for some reason. The sample code is below.public class AppRegistration { public int AppRegistrationId { get; set; } [Required] [StringLength(50)] [Display(Name = "Username")] public string UserName { get; set; } [Required] [StringLength(100)] public string Password { get; set; } [StringLength(20)] public string StudentOrAgent { get; set; } // navigation properties public virtual AppStatus AppStatus { get; set; } public virtual Agreement Agreement { get; set; } public virtual AnotherTable AnotherTable { get; set; } }
The dependent table with a foreign key is below.
public class Agreement { [Key] [ForeignKey("AppRegistration")] public int AppRegistrationId { get; set; } public DateTime DateAgreed { get; set; } public virtual AppRegistration AppRegistration { get; set; } }
When I try to delete an entry from the generated
AppRegistrations
table I get a Reference constraint conflict.I tried putting
[Required]
on the navigation property in the dependent table but it doesn't do anything - theUpdate-Database
command shows theNo pending code-based migrations.
message. Any ideas? Thanks.Update:I'm getting the following error message:
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.AppStatus_dbo.AppRegistrations_AppRegistrationId". The conflict occurred in database "MVCapp", table "dbo.AppStatus", column 'AppRegistrationId'.
解决方案I decided to work out the
cascade delete
problem in a separate sample project. I found the following blog & MSDN pages very useful.- http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/
- http://msdn.microsoft.com/en-us/library/gg671256%28v=VS.103%29.aspx
- http://msdn.microsoft.com/en-us/library/gg671273%28v=VS.103%29.aspx
Using the
Code First
approach create the following Model.public class Category { public int CategoryId { get; set; } public string CategoryName { get; set; } public virtual Book Book { get; set; } } public class Book { public int CategoryId { get; set; } public string BookTitle { get; set; } public string BookAuthor { get; set; } public string BookISBN { get; set; } public virtual Category Category { get; set; } }
(I realize the entity names suggest one-to-many relationship, but I am trying to model 1-to-1 relationship, as in my original question at the top.)
So, in the above model each Category can only have one Book.
In your
DbContext
-derived class add the following.protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<Book>() .HasKey(t => t.CategoryId); modelBuilder.Entity<Category>() .HasRequired(t => t.Book) .WithRequiredPrincipal(t => t.Category) .WillCascadeOnDelete(true); }
(The following namespaces are required for the above code:
System.Data.Entity
,System.Data.Entity.ModelConfiguration.Conventions
.)This properly creates the 1-to-1 relationship. You'll have a primary key in each table and also a foreign key in
Book
table withON DELETE CASCADE
enabled.In the above code, on the
Category
entity I usedWithRequiredPrincipal()
witht => t.Category
argument, where the argument is the foreign key column in the dependent table.If you use
WithRequiredPrincipal()
without an argument you'll get an extra column in theBook
table and you'll have two foreign keys in theBook
table pointing toCategoryId
inCategory
table.I hope this info helps.
UPDATE
Later on I found answer directly here:
http://msdn.microsoft.com/en-us/data/jj591620#RequiredToRequired
这篇关于代码第一个一对一启用级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!