CodeDemen
CodeDemen

Reputation: 1971

EF Foreign Keys migration throws error

Ive got 2 tables:

[Table("Ticket_Admins")]
public class TicketAdmin
{
    public TicketAdmin()
    {
        Name = new Record();
        Name.IsRevisioned = false;
        Name.IsVisible = true;
        Name.Category = typeof(TicketAdmin).Name;
    }
    [Key]
    public int Id { get; set; }
    public virtual Record Name { get; set; }
    public virtual TicketPost Post { get; set; }
    public virtual UserProfile User { get; set; }
}

[Table("UserProfile")]
public class UserProfile
{
    private string _email;

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]       
    public int UserId { get; set; }
    public string Email 
    {
        get { return _email; }
        set { _email = value.ToLower(); }
    }
}

And map them in context:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TicketAdmin>()
            .HasRequired(x => x.User)
            .WithRequiredPrincipal();
        base.OnModelCreating(modelBuilder);
    }

But on Migration I Am getting error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.UserProfile_dbo.Ticket_Admins_UserId". The conflict occurred in database "aspnet-BoomzHelperPortal-dbv3", table "dbo.Ticket_Admins", column 'Id'.

Am I doing something wrong here? What I am exactly trying to do is to Map My TicketAdmin to UserProfile by id (UserProfile.UserId = TicketAdmin.Id)

Thak you for your time!

UPDATE: Ive just tried to create an empty db eith this shema an found out that it makes UserProfile.UserId a foreign key column but what i need is TicketAdmin.Id to become a FK column. (Its like all users have UserProfile, but not everybody has TicketAdmin Settings, And TicketAdmin is a child of UserProfile and has the same ID as the UserProfile ID).

Upvotes: 1

Views: 5925

Answers (4)

InkHeart
InkHeart

Reputation: 880

If you are trying to create new Foreign key for an Existence table, you need to remove all the data in the existence table. Since every record needs to have a foreign key, while there are some records that already created, but had no foreign key.

Upvotes: 0

CodeDemen
CodeDemen

Reputation: 1971

Ok, I figured out all I needed is to set ForeignKey like:

[Table("Ticket_Admins")]
public class TicketAdmin
{
    public TicketAdmin()
    {
        Name = new Record();
        Name.IsRevisioned = false;
        Name.IsVisible = true;
        Name.Category = typeof(TicketAdmin).Name;
    }
    [Key]
    public int Id { get; set; }
    public virtual Record Name { get; set; }
    public virtual TicketPost Post { get; set; }
    [ForeignKey("UserId")]
    public virtual UserProfile User { get; set; }
}

And thats all. Now thw foreign key generates propertly.

Upvotes: 2

Rodders
Rodders

Reputation: 2435

This is because you are adding a foreign key to a table that already contains data that does not match this constraint. Try either deleting the data (if possible) or modifying the data to suit the constraint or otherwise, you'll need to modify the constraint

Also, it looks like you are trying to add a foreign key between two primary keys, I don't know if that's even possible, consider modifying your Ticket_Admins table to include a UserId field:

[Table("Ticket_Admins")]
public class TicketAdmin
{
    public TicketAdmin()
    {
        Name = new Record();
        Name.IsRevisioned = false;
        Name.IsVisible = true;
        Name.Category = typeof(TicketAdmin).Name;
    }
    [Key]
    public int Id { get; set; }
    public int UserId { get; set; }
    public virtual Record Name { get; set; }
    public virtual TicketPost Post { get; set; }
    public virtual UserProfile User { get; set; }
}

and then create a foreign key between UserProfile.UserId = Ticket_Admins.UserId

Upvotes: 2

Nuffin
Nuffin

Reputation: 3972

If you want to add a required relation (non-nullable foreign key), one of the following conditions must be true:

  • The table containing the foreign key (Ticket_Admins in this case) has to be empty, or
  • the column for the foreign key existed before, and every value in said column contains a valid key for a related entity

Upvotes: 6

Related Questions