Here is a complete console application with the configuration you need:
class Program
{
static void Main(string[] args)
{
var ctx = new TesteContext();
ctx.Database.CreateIfNotExists();
Console.ReadKey();
}
}
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
public class ParentConfiguration : EntityTypeConfiguration<Parent>
{
public ParentConfiguration()
{
HasKey(x => x.ParentId);
}
}
public class ChildConfiguration : EntityTypeConfiguration<Child>
{
public ChildConfiguration()
{
HasKey(x => x.ChildId);
HasRequired(x => x.Parent)
.WithMany()
.HasForeignKey(x => x.ParentId)
.WillCascadeOnDelete(false);
}
}
public class TesteContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Childs { get; set; }
public TesteContext() : base("Teste_123")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ParentConfiguration());
modelBuilder.Configurations.Add(new ChildConfiguration());
}
}
And add the connection string
to you app.config
: (Replacing the database, etc)
<connectionStrings>
<add name="Teste_123" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Teste_123;Integrated Security=True;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
With navigation property and without EntityTypeConfiguration
Just run the code:
class Program
{
static void Main(string[] args)
{
var ctx = new TesteContext();
ctx.Database.CreateIfNotExists();
Console.ReadKey();
}
}
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
}
public class TesteContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Childs { get; set; }
public TesteContext() : base("Teste_123")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Child>()
.HasRequired(x => x.Parent)
.WithMany();
}
}
Without navigation property
You can add a migration:
enable-migrations
With the code below:
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
}
public class ParentConfiguration : EntityTypeConfiguration<Parent>
{
public ParentConfiguration()
{
HasKey(x => x.ParentId);
}
}
public class ChildConfiguration : EntityTypeConfiguration<Child>
{
public ChildConfiguration()
{
HasKey(x => x.ChildId);
}
}
public class TesteContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Childs { get; set; }
public TesteContext() : base("Teste_123")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ParentConfiguration());
modelBuilder.Configurations.Add(new ChildConfiguration());
}
}
If you add a migration:
Add-Migration FirstMigration
You will get the code below:
public partial class FirstMigration : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Children",
c => new
{
ChildId = c.Int(nullable: false, identity: true),
Name = c.String(),
ParentId = c.Int(nullable: false),
})
.PrimaryKey(t => t.ChildId);
CreateTable(
"dbo.Parents",
c => new
{
ParentId = c.Int(nullable: false, identity: true),
Name = c.String(),
})
.PrimaryKey(t => t.ParentId);
}
public override void Down()
{
DropTable("dbo.Parents");
DropTable("dbo.Children");
}
}
Just add manually on the up method:
AddForeignKey("dbo.Children", "ParentId", "dbo.Parents", "ParentId", cascadeDelete: false);
And you'll get:
public partial class FirstMigration : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Children",
c => new
{
ChildId = c.Int(nullable: false, identity: true),
Name = c.String(),
ParentId = c.Int(nullable: false),
})
.PrimaryKey(t => t.ChildId);
CreateTable(
"dbo.Parents",
c => new
{
ParentId = c.Int(nullable: false, identity: true),
Name = c.String(),
})
.PrimaryKey(t => t.ParentId);
AddForeignKey("dbo.Children", "ParentId", "dbo.Parents", "ParentId", cascadeDelete: false);
}
public override void Down()
{
DropTable("dbo.Parents");
DropTable("dbo.Children");
}
}
Now, when you run update-database
, you'll get what you want:
