English 中文(简体)
与签名KEY冲突
原标题:Conflicted with the FOREIGN KEY

错误发生在保留目标对夫妇时。

Code

<>Data>

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

www.un.org/Depts/DGACM/index_spanish.htm 背景和组合

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

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

如何理解<代码> 可查阅

Error

http://www.ohchr.org。

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.

问题回答

根据这一法典:

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

你们要求所有用户都“联合起来”。

因此,当你们作出改变时,你创造的所有使用者是否都结婚? 否则,就消除了这一外国关键制约因素。 (法官离开夫妇,你应为O.K.)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签