I analyzed the problem in detail during the preparation of the answer , and I can offer you two solutions to the problem.
The problem exists due to two properties in the Match class
public int HomeTeamId { get; set; } public int GuestTeamId { get; set; }
and the foreign keys HomeTeamId and GuestTeamId that will be generated. EF7 will generate foreign keys using ON DELETE CASCADE , which can no longer be used as a single foreign key. The current implementation of Entity Framework (RC1) does not have an annotation attribute that you can use to change behavior.
The first solution to this problem is to use properties with a null value, such as
public int? HomeTeamId { get; set; } public int? GuestTeamId { get; set; }
or
public int HomeTeamId { get; set; } public int? GuestTeamId { get; set; }
The maximum single property must be invalid. As a result, the problem will be solved, but it will have a slight flaw, which may not be important for some scenarios. A field in the database table for the nullable property will not have the NOT NULL property in the column definition.
If you need to hold both HomeTeamId and GuestTeamId non-nullable, then you can solve the problem by changing the context class (inherited from DbContext) where the Match and Team classes will be used.
You already have a specific context class line below
public class MyDBContext : DbContext { DbSet<Team> Teams { get; set; } DbSet<Match> Matches { get; set; } }
To solve the description problem, you can add protected OnModelCreating to a class that explicitly sets
public class MyDBContext : DbContext { protected override void OnModelCreating(ModelBuilder modelbuilder) { base.OnModelCreating(modelbuilder); modelbuilder.Entity(typeof (Match)) .HasOne(typeof (Team), "GuestTeam") .WithMany() .HasForeignKey("GuestTeamId") .OnDelete(DeleteBehavior.Restrict);
You can use the DeleteBehavior.Restrict reason for both foreign keys (instead of using DeleteBehavior.Cascade on one). It is important to note that the latter approach allows you to keep both HomeTeamId and GuestTeamId , as well as the corresponding fields in the database, as non-empty.
See the documentation for more information.