I have following two entities
public class User
{
[Key]
public int Id { get; set; } // PK
// for many-to-many relation with Booking.Members
public virtual ICollection<Booking> Bookings { get; set; }
}
public class Booking
{
public Booking()
{
Members = new List<User>();
}
public int Id { get; set; } // PK
[ForeignKey("UserByCreated")]
public int UserByCreatedId { get; set; } // FK
public virtual User UserByCreated { get; set; }
// for many-to-many relation with User.Bookings
public virtual ICollection<User> Members { get; set; }
}
As shown above, User and Booking have two different relationships, one is many-to-many and the other is foreign key relationship.
What I want to do is having a UserByCreatedId foreign key column with NOT NULL condition in Bookings table.
However, it seems not possible due to another relationship with User.
Is there any solution for this?
Use the Fluent API to configure the relationships because EF fails to identify the relationship ends when there are multiple relationships between two entities.
Edit
The
UserByCreatedrelationship needs to be changed to optional to avoid the multiple delete paths problem.