0

我正在使用实体框架代码优先方法在 asp.net 4.5 中设计我的 Web 应用程序。我已经在代码优先实体中转换了所有与 asp.net 成员相关的表,如下所示

public class Application
    {

        public string ApplicationName { get; set; }
        [Key]
        public System.Guid ApplicationId { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Membership> Memberships { get; set; }
        public virtual ICollection<Role> Roles { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }

    public class Membership
    {
        [ForeignKey("Application")]
        public System.Guid ApplicationId { get; set; }
        [Key, ForeignKey("User")]
        public System.Guid UserId { get; set; }
        public string Password { get; set; }
        public int PasswordFormat { get; set; }
        public string PasswordSalt { get; set; }
        public string Email { get; set; }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public bool IsApproved { get; set; }
        public bool IsLockedOut { get; set; }
        public System.DateTime CreateDate { get; set; }
        public System.DateTime LastLoginDate { get; set; }
        public System.DateTime LastPasswordChangedDate { get; set; }
        public System.DateTime LastLockoutDate { get; set; }
        public int FailedPasswordAttemptCount { get; set; }
        public System.DateTime FailedPasswordAttemptWindowStart { get; set; }
        public int FailedPasswordAnswerAttemptCount { get; set; }
        public System.DateTime FailedPasswordAnswerAttemptWindowsStart { get; set; }
        public string Comment { get; set; }
        public virtual Application Application { get; set; }
        public virtual User User { get; set; }
    }

    public class Profile
    {
        [Key, ForeignKey("User")]
        public System.Guid UserId { get; set; }
        public string PropertyNames { get; set; }
        public string PropertyValueStrings { get; set; }
        public byte[] PropertyValueBinary { get; set; }
        public System.DateTime LastUpdatedDate { get; set; }
        public virtual User User { get; set; }
    }

    public  class Role
    {
        [ForeignKey("Application")]
        public System.Guid ApplicationId { get; set; }
        [Key]
        public System.Guid RoleId { get; set; }
        public string RoleName { get; set; }
        public string Description { get; set; }

        public virtual Application Application { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }

    public  class User
    {
        [ForeignKey("Application")]
        public System.Guid ApplicationId { get; set; }
        [Key]
        public System.Guid UserId { get; set; }
        public string UserName { get; set; }
        public bool IsAnonymous { get; set; }
        public System.DateTime LastActivityDate { get; set; }

        public virtual Application Application { get; set; }
        public virtual Membership Membership { get; set; }
        public virtual Profile Profile { get; set; }
        public virtual ICollection<Role> Roles { get; set; }
    }

我的 Datacontext 和 initalizer 类如下

 public class TrainningInitializer : DropCreateDatabaseIfModelChanges<TrainningContext>
    {
        protected override void Seed(TrainningContext context)
        {

        }

    }

 public partial class TrainningContext : DbContext
    {
        public TrainningContext()
            : base("name=TrainningContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

        }

        public DbSet<Application> Applications { get; set; }
        public DbSet<Membership> Memberships { get; set; }
        public DbSet<Profile> Profiles { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }


    }

我在 Global.asax 内的 application_start 事件中设置初始化程序如下。

Database.SetInitializer<TrainningContext>(new TrainningInitializer());

但是当我运行我的应用程序时,我遇到了错误

Introducing FOREIGN KEY constraint 'FK_dbo.RoleUsers_dbo.Users_User_UserId' on table 'RoleUsers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

有人可以帮助纠正我的实体类。

4

1 回答 1

0

当您删除一个Application实体时,此删除级联到RolesUsers,然后从RolesRoleUsers连接表,从UsersRoleUsersApplication从表到表的这两个删除路径RoleUsers是异常正在谈论的“多个级联路径”。SQL Server 中不允许使用它们。

RolesUsers关系默认启用级联删除,Application因为这两个表中的外键ApplicationId不可为空,因此需要关系。默认情况下,所需关系已打开级联删除。

但是您可以使用 Fluent API 将其关闭:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Application>()
        .HasMany(a => a.Roles)
        .WithRequired(r => r.Application)
        .HasForeignKey(r => r.ApplicationId)
        .WillCascadeOnDelete(false);

    modelBuilder.Entity<Application>()
        .HasMany(a => a.Users)
        .WithRequired(u => u.Application)
        .HasForeignKey(u => u.ApplicationId)
        .WillCascadeOnDelete(false);
}

(对于两者之一可能就足够了。)

如果您Application现在要删除一个实体,您还需要删除相关的Rolesand Users。数据库将不再为您自动执行此操作。

于 2013-05-16T20:13:55.293 回答