这是一个完整的控制台应用程序,其中包含您需要的配置:
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());
}
}
并添加connection string 给你app.config:(替换数据库等)
<connectionStrings>
<add name="Teste_123" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Teste_123;Integrated Security=True;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
有导航属性,没有EntityTypeConfiguration
只需运行代码:
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();
}
}
您可以添加迁移:
enable-migrations
使用下面的代码:
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());
}
}
如果您添加迁移:
Add-Migration FirstMigration
你会得到下面的代码:
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");
}
}
只需在 up 方法上手动添加:
AddForeignKey("dbo.Children", "ParentId", "dbo.Parents", "ParentId", cascadeDelete: false);
你会得到:
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");
}
}
现在,当你运行 update-database 时,你会得到你想要的: