与FOREIGN KEY冲突

时间:2011-10-17 15:04:29

标签: c# entity-framework-4.1 ef-code-first entity-relationship

保存对象时发生错误。

代码

数据类

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);
    }
}

问题

不知道配置是否正确,但我们与情侣用户的关系是:1到1。

不太了解WithRequiredPrincipalWithRequiredDependent

的方式

错误

在SaveChanges() =&gt;

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.

1 个答案:

答案 0 :(得分:0)

使用以下代码:

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

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

那么,当您提交更改时,您创建的所有用户都已结婚吗?如果不是,请删除该外键约束。 (只要留下几个,你应该是O.K。)