我正在使用实体框架代码优先方法在 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.
有人可以帮助纠正我的实体类。