与外键冲突

发布于 2024-12-10 15:13:47 字数 4052 浏览 0 评论 0原文

保存对象对时出现错误。

代码

数据类

public class User
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public bool IsApproved { get; set; }
    public bool IsBlock { get; set; }
    public bool IsGuest { get; set; }
    public string CodeGuest { get; set; }
    public Gender Gender { get; set; }
    public DateTime? Birth { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual Couple Couple { get; set; }

    public User()
    {
        Id = Guid.NewGuid();
    }
}


public class Couple
{
    public Guid Id { get; private set; }
    public string UrlKeyword { get; set; }
    public virtual User Groom { get; set; }
    public virtual User Bride { get; set; }
    public DateTime? Marriage { get; set; }
    public DateTime? Dating { get; set; }
    public DateTime? Engagement { get; set; }

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

    public Couple()
    {
        Id = Guid.NewGuid();
    }
}

上下文和配置

public class DataContext : DbContext
{
    #region Collections

    public DbSet<Role> Roles { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Couple> Couples { get; set; }

    #endregion

    public DataContext()
    {
        Database.SetInitializer(new AndMarriedInitializer());

        if (!Database.CreateIfNotExists())
            Database.CreateIfNotExists();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
        base.OnModelCreating(modelBuilder);
    }
}

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(p => p.Id).
            Property(p => p.Id)
                .IsRequired();

        Property(p => p.FirstName)
            .HasMaxLength(60)
            .IsRequired();

        Property(p => p.LastName)
            .HasMaxLength(120)
            .IsRequired();

        Property(p => p.Email)
            .HasMaxLength(120)
            .IsRequired();

        Property(p => p.Password)
            .HasMaxLength(60);

        Property(p => p.CodeGuest)
            .HasMaxLength(60);

        HasRequired(u => u.Couple).WithRequiredPrincipal();
    }
}

public class CoupleConfiguration : EntityTypeConfiguration<Couple>
{
    public CoupleConfiguration()
    {
        HasKey(p => p.Id)
            .Property(p => p.Id)
            .IsRequired();

        Property(p => p.UrlKeyword)
            .IsRequired()
            .HasMaxLength(25);

        HasRequired(p => p.Groom).WithRequiredPrincipal().WillCascadeOnDelete();
        HasRequired(p => p.Bride).WithRequiredPrincipal().WillCascadeOnDelete();
    }
}

public class AndMarriedInitializer : DropCreateDatabaseIfModelChanges<DataContext>
{
    protected override void Seed(DataContext context)
    {
        context.Roles.Add(new Role
                              {
                                  Name = Constants.RoleAdmin
                              });
        context.Roles.Add(new Role
                              {
                                  Name = Constants.RoleCouple
                              });
        context.Roles.Add(new Role
                              {
                                  Name = Constants.RoleGuest
                              });

        context.SaveChanges();

        base.Seed(context);
    }
}

问题

不知道配置是否正确,但是我们与Couple with User的关系是:1对1。

不太明白WithRequiredPrincipalWithRequiredDependent

错误

On SaveChanges() =>

INSERT 语句与 FOREIGN KEY 约束“User_Couple”冲突。冲突发生在数据库“andmarried”、表“dbo.Users”、列“Id”中。 该声明已终止。

The error occurs when saving the object couple.

Code

Data class

public class User
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public bool IsApproved { get; set; }
    public bool IsBlock { get; set; }
    public bool IsGuest { get; set; }
    public string CodeGuest { get; set; }
    public Gender Gender { get; set; }
    public DateTime? Birth { get; set; }
    public virtual ICollection<Role> Roles { get; set; }
    public virtual Couple Couple { get; set; }

    public User()
    {
        Id = Guid.NewGuid();
    }
}


public class Couple
{
    public Guid Id { get; private set; }
    public string UrlKeyword { get; set; }
    public virtual User Groom { get; set; }
    public virtual User Bride { get; set; }
    public DateTime? Marriage { get; set; }
    public DateTime? Dating { get; set; }
    public DateTime? Engagement { get; set; }

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

    public Couple()
    {
        Id = Guid.NewGuid();
    }
}

Context and configurations

public class DataContext : DbContext
{
    #region Collections

    public DbSet<Role> Roles { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Couple> Couples { get; set; }

    #endregion

    public DataContext()
    {
        Database.SetInitializer(new AndMarriedInitializer());

        if (!Database.CreateIfNotExists())
            Database.CreateIfNotExists();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
        base.OnModelCreating(modelBuilder);
    }
}

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(p => p.Id).
            Property(p => p.Id)
                .IsRequired();

        Property(p => p.FirstName)
            .HasMaxLength(60)
            .IsRequired();

        Property(p => p.LastName)
            .HasMaxLength(120)
            .IsRequired();

        Property(p => p.Email)
            .HasMaxLength(120)
            .IsRequired();

        Property(p => p.Password)
            .HasMaxLength(60);

        Property(p => p.CodeGuest)
            .HasMaxLength(60);

        HasRequired(u => u.Couple).WithRequiredPrincipal();
    }
}

public class CoupleConfiguration : EntityTypeConfiguration<Couple>
{
    public CoupleConfiguration()
    {
        HasKey(p => p.Id)
            .Property(p => p.Id)
            .IsRequired();

        Property(p => p.UrlKeyword)
            .IsRequired()
            .HasMaxLength(25);

        HasRequired(p => p.Groom).WithRequiredPrincipal().WillCascadeOnDelete();
        HasRequired(p => p.Bride).WithRequiredPrincipal().WillCascadeOnDelete();
    }
}

public class AndMarriedInitializer : DropCreateDatabaseIfModelChanges<DataContext>
{
    protected override void Seed(DataContext context)
    {
        context.Roles.Add(new Role
                              {
                                  Name = Constants.RoleAdmin
                              });
        context.Roles.Add(new Role
                              {
                                  Name = Constants.RoleCouple
                              });
        context.Roles.Add(new Role
                              {
                                  Name = Constants.RoleGuest
                              });

        context.SaveChanges();

        base.Seed(context);
    }
}

Question

Do not know if configured correctly, but we relate to the Couple with User is: 1 to 1.

Do not quite understand how the WithRequiredPrincipal and WithRequiredDependent

Error

On SaveChanges() =>

The INSERT statement conflicted with the FOREIGN KEY constraint "User_Couple". The conflict occurred in database "andmarried", table "dbo.Users", column 'Id'.
The statement has been terminated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-12-17 15:13:47

使用这行代码:

public UserConfiguration()
{
//...
    HasRequired(u => u.Couple).WithRequiredPrincipal();
//...

您要求所有用户始终“耦合”。

那么,当您提交更改时,您创建的所有用户都已结婚吗?如果不是,请删除该外键约束。 (只要把那几个留在里面就应该没问题了)

With this line of code:

public UserConfiguration()
{
//...
    HasRequired(u => u.Couple).WithRequiredPrincipal();
//...

You're requiring all users to be "coupled" all the time.

So, when you commit your changes, are all the users you created married? If not, remove that foreign key constraint. (Just leave the couple ones in and you should be O.K.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文