简体   繁体   中英

Unwanted Foreign Key Constraint in Entity Framework 6.1 Code First

My EF project has a Day class, a WeekEnd class and a Week class in my model. I marked the classes with [InverseProperty] and [Required] attributes:

public class Day
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int DayId { get; set; }

    public DateTime UtcDayStart { get; set; }

    public string DayName { get; set; }

    public int ISOWeekNumber {get; set; }

    [InverseProperty("Days")]
    public virtual Week Week { get; set; }

    [InverseProperty("Days")]
    public virtual WeekEnd WeekEnd { get; set; }
}

public class WeekEnd
{
    Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int WeekEndId { get; set; }

    public DateTime UtcWeekEndStart { get; set; }

    public int ISOWeekNumber { get; set; }

    [Required]
    [InverseProperty("WeekEnd")]
    public virtual Week Week { get; set; }

    [InverseProperty("WeekEnd")]
    public virtual ICollection<Day> Days { get; set; }
}

public class Week
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int WeekId { get; set; }

    public DateTime UtcWeekStart { get; set; }

    public int ISOWeekNumber { get; set; }

    [InverseProperty("Week")]
    public virtual WeekEnd WeekEnd { get; set; }

    [InverseProperty("Week")]
    public virtual ICollection<Day> Days { get; set; }
}

The problem is that SQL Server won't let me insert Day table data with a Null for WeekEnd_WeekEndId . And since not all days of the week belong to week ends that is a problem for me.

The Insert statement:

INSERT INTO [database].[dbo].[Days] (DayId, UtcDayStart, ISOWeekNumber,
DayName, WeekEnd_WeekEndId, Week_WeekId)
VALUES ('3', '1/3/2005 12:00 AM', '1', 'Monday', '', '1')

The SQL Server error message is:

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Days_dbo.WeekEnds_WeekEnd_WeekEndId". The conflict occurred in database "database", table "dbo.WeekEnds", column 'WeekEndId'.

How can I set the attributes or possibly add a foreign key column (or columns) to make the code-first migration not create that foreign key constraint between Days and WeekEnds ?

EDIT:

So after working on this and trying different FluentAPI statements and attributes I think the model is fine. It is the SQL Server INSERT statement that won't work. If I do it this way:

INSERT INTO [database].[dbo].[Days] (DayId, UtcDayStart, ISOWeekNumber,
DayName, Week_WeekId)
VALUES ('3', '1/3/2005 12:00 AM', '1', 'Monday', '1')

and then do an UPDATE like the following on those days that actually belong to a WeekEnd :

UPDATE [database].[dbo].[Days] SET WeekEnd_WeekEndId = 'X'  WHERE DayId ='Y'

Then my table has Null values (like I want) for WeekEnd_WeekEndId for Monday through Friday days.

I'm not sure that this would qualify as an answer though because I'm new to this. So I will let someone else propose an answer.

In order to configure the relationships as a 0..many instead of a 1..many , you will need to adjust the model. The easiest way to do this would be with the FluentAPI.

protected override void OnModelCreating(DbModelBuilder modelBuilder) {

    modelBuilder.Entity<Week>()
                .HasMany<Day>(w => w.Days)
                .WithOptional(d => d.Week);

    modelBuilder.Entity<WeekEnd>()
                .HasMany<Day>(we => we.Days)
                .WithOptional(d => d.WeekEnd);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM