You may be the victim of EF Code-First mapping agreements that automatically create relationships between NationAllies and toNation that you do not want to have.
If I understand you correctly (but I'm not 100 percent sure if I do this), you really want to have two connections, and you have only one end of the relationship in each of the entities. Thus, NationAllies DOES NOT NationAllies to toNation , but to the "invisible" owner country in your NationAlly organization.
If so, you need to explicitly rewrite convention mappings. In Fluent API EF 4.1, it might look like this:
public class MyContext : DbContext { public DbSet<Nation> Nations { get; set; } public DbSet<NationAlly> NationAllies { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Nation>() .HasMany(n => n.NationAllies) .WithRequired() .Map(conf => conf.MapKey("OwnerID")) .WillCascadeOnDelete(false); modelBuilder.Entity<NationAlly>() .HasRequired(a => a.toNation) .WithMany() .Map(conf => conf.MapKey("NationID")) .WillCascadeOnDelete(false); } }
This mapping would create two foreign keys, OwnerID and NationID in the NationAllies table, pointing to the primary ID key in the Nations table.
Edit
Here is the application I tested:
- Create a new console application in VS2010 / .NET 4.0, name it "NationsApp"
- Add link to "EntityFramework.dll"
- Clear the contents of "Program.cs" and paste the following in its place:
Program.cs Content:
using System; using System.Collections.Generic; using System.Data.Entity; namespace NationsApp { public class Nation { public int ID { get; set; } public int name { get; set; } public List<NationAlly> NationAllies { get; set; } } public class NationAlly { public int ID { get; set; } public int level { get; set; } public Nation toNation { get; set; } } public class NationsContext : DbContext { public DbSet<Nation> Nations { get; set; } public DbSet<NationAlly> NationAllies { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Nation>() .HasMany(n => n.NationAllies) .WithRequired() .Map(conf => conf.MapKey("OwnerID")) .WillCascadeOnDelete(false); modelBuilder.Entity<NationAlly>() .HasRequired(a => a.toNation) .WithMany() .Map(conf => conf.MapKey("NationID")) .WillCascadeOnDelete(false); } } class Program { static void Main(string[] args) { using (var context = new NationsContext()) { try {
You can set a breakpoint on "throw" to see possible exceptions in e in the debugger.
This creates a database called NationsApp.NationsContext if you are using SQL Server Express and do not have any additional connection strings.
It gives two relations Nation_NationAllies (FK - "OwnerID") and NationAlly_toNation (FK - "NationID"). All columns are not NULL. The result in the database is as follows:

Slauma
source share