同一个表中的两个外键,其中一个不恰当地具有cascadeDelete:true。为什么?

时间:2014-03-04 18:12:16

标签: entity-framework code-first entity-framework-6 ef-migrations

以下是OrganizationMarker的模型:

using System;
using System.Collections.Generic;
using System.Data.Entity.Spatial;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MyApp.Models
{
    public class Orgainzation
    {
        public int ID { get; set; }

        [Required]
        [MaxLength(50, ErrorMessage = "Name cannot be longer than 20 characters.")]
        public string Name{ get; set; }

        [Required]
        public int MarkerId { get; set; }

        [Required]
        public int SystemMarkerId { get; set; }

        public virtual Marker Marker{ get; set; }

        [ForeignKey("SystemMarkerId")]
        public virtual Marker SystemMarker { get; set; }   
    }

    public class Marker
    {
        public int ID { get; set; }

        [Required]
        [Display(Name="Marker")]
        public string MarkerName{ get; set; }

        public virtual ICollection<Organization> Organizations { get; set; }

        [InverseProperty("SystemMarkerId")]
        public virtual ICollection<Organization> SystemOrganizations { get; set; }
    }
}

以下DbContext覆盖了OnModelCreating

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MyApp.Models
{
    public class MyAppDb : DbContext
    {
        public MyAppDb() : base("DefaultContextDb") { }

        public DbSet<MyApp.Models.Organization> Organizations { get; set; }
        public DbSet<MyApp.Models.Marker> Markers { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<MyApp.Models.Organization>()
                .HasRequired(x => x.Marker).WithMany(x => x.Organizations).WillCascadeOnDelete(false);

            modelBuilder.Entity<MyApp.Models.Organization>()
                .HasRequired(x => x.SystemMarker).WithMany(x => x.SystemOrganizations).WillCascadeOnDelete(false);
        }
    }
}

以下是在外键cascadeDelete: true上使用不恰当的SystemMarkerId设置创建的迁移:

    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Organization",
                c => new
                    {
                        ID = c.Int(nullable: false, identity: true),
                        Name= c.String(maxLength: 50),
                        MarkerId = c.Int(nullable: false),
                        SystemMarkerId = c.Int(nullable: false),


                    })
                .PrimaryKey(t => t.ID)
                .ForeignKey("dbo.Markers", t => t.MarkerId)
                .ForeignKey("dbo.Markers", t => t.SystemMarkerId, cascadeDelete: true)
                .Index(t => t.MarkerId)
                .Index(t => t.SystemMarkerId);
        }
    }

为什么会这样做?如何让它停止?

0 个答案:

没有答案