Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当只有model1引用其他模型时,如何使用fluent API添加外键?_C#_Asp.net Core_Ef Core 2.2 - Fatal编程技术网

C# 当只有model1引用其他模型时,如何使用fluent API添加外键?

C# 当只有model1引用其他模型时,如何使用fluent API添加外键?,c#,asp.net-core,ef-core-2.2,C#,Asp.net Core,Ef Core 2.2,如何使用fluent api添加外键 例如 模型1 id,name,model2.id 模型2 身份证,姓名 我已经阅读了关于构建一对一关系的内容,但是这个示例显示model1只引用model2 我创建了一个演示,下面模型1是员工,模型2是部门,员工引用了部门 型号: public class Employee { [Key] public int Id { get; set; } public string Name { get; set; } public

如何使用fluent api添加外键

例如

模型1

id,name,model2.id

模型2

身份证,姓名


我已经阅读了关于构建一对一关系的内容,但是这个示例显示model1只引用model2

我创建了一个演示,下面模型1是
员工
,模型2是
部门
员工
引用了
部门

型号:

public class Employee
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    public int DepartmentId { get; set; }
    public Department Department { get; set; }
}

public class Department
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

}
dcContext:

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Employee>()
        .HasOne(e => e.Department)
        .WithOne()
        .HasForeignKey<Employee>(e => e.DepartmentId);

    }
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.HasOne(e=>e.Department)
.WithOne()
.HasForeignKey(e=>e.DepartmentId);
}
结果:

migrationBuilder.CreateTable(
            name: "Employees",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                Name = table.Column<string>(nullable: true),
                DepartmentId = table.Column<int>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Employees", x => x.Id);
                table.ForeignKey(
                    name: "FK_Employees_Department_DepartmentId",
                    column: x => x.DepartmentId,
                    principalTable: "Department",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            })
migrationBuilder.CreateTable(
姓名:“员工”,
列:表=>new
{
Id=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn),
Name=table.Column(可空:true),
DepartmentId=表.列(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_员工”,x=>x.Id);
表1.外键(
名称:“FK\U员工\部门\部门ID”,
列:x=>x.DepartmentId,
原则性:“部门”,
主栏:“Id”,
onDelete:引用。级联);
})