Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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# 实体框架中的代码优先方法是否总是创建Id字段,即使HasKey用于标识主键?_C#_Sql Server_Entity Framework_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 实体框架中的代码优先方法是否总是创建Id字段,即使HasKey用于标识主键?

C# 实体框架中的代码优先方法是否总是创建Id字段,即使HasKey用于标识主键?,c#,sql-server,entity-framework,asp.net-core,entity-framework-core,C#,Sql Server,Entity Framework,Asp.net Core,Entity Framework Core,我在EntityFrameworkCore2.2中有一对一的关系,其中我使用Fluent APIHasKey()识别两个实体中的主键。添加迁移并更新Sql Server数据库时,将创建标识的主键以及Id列。Id列是否总是被创建,即使它实际上不包含任何数据,或者我是否遗漏了什么 Profile.cs public类配置文件:实体,IAggregateRoot { 公共int ProfileId{get;set;} 公众人物{get;set;} 受保护的配置文件(){} } 配置文件DbConte

我在EntityFrameworkCore2.2中有一对一的关系,其中我使用Fluent API
HasKey()
识别两个实体中的主键。添加迁移并更新Sql Server数据库时,将创建标识的主键以及Id列。Id列是否总是被创建,即使它实际上不包含任何数据,或者我是否遗漏了什么

Profile.cs

public类配置文件:实体,IAggregateRoot
{
公共int ProfileId{get;set;}
公众人物{get;set;}
受保护的配置文件(){}
}
配置文件DbContext配置

public void配置(EntityTypeBuilder)
{
HasKey(p=>p.ProfileId);
builder.Property(o=>o.ProfileId)
.ValueGeneratedOnAdd()
.IsRequired();
builder.HasOne(p=>p.Person)
.WithOne(s=>s.Profile)
.HasForeignKey(s=>s.ProfileId);
}
Person.cs

公共类人物:实体,IAggregateRoot
{
public int UserId{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共日期时间出生日期{get;set;}
公共地址邮件地址{get;private set;}
公共字符串移动电话{get;set;}
公共int ProfileId{get;set;}
公共配置文件{get;set;}
受保护人(){}
}
persondbcontext配置

public void配置(EntityTypeBuilder)
{
HasKey(p=>p.UserId);
builder.Property(p=>p.UserId)
.ValueGeneratedNever();
builder.Property(p=>p.FirstName)
.IsRequired()
.HasMaxLength(50);
builder.Property(p=>p.LastName)
.IsRequired()
.HasMaxLength(50);
builder.Property(p=>p.DateOfBirth)
.IsRequired();
builder.OwnsOne(p=>p.MailingAddress);
builder.Property(p=>p.MobilePhone)
.HasMaxLength(20);
}
迁移结果

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "Profile",
        columns: table => new
        {
            ProfileId = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            Id = table.Column<int>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Profile", x => x.ProfileId);
        });

    migrationBuilder.CreateTable(
        name: "Test",
        columns: table => new
        {
            TestId = table.Column<int>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            Id = table.Column<int>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Test", x => x.TestId);
        });

    migrationBuilder.CreateTable(
        name: "Person",
        columns: table => new
        {
            UserId = table.Column<int>(nullable: false),
            Id = table.Column<int>(nullable: false),
            FirstName = table.Column<string>(maxLength: 50, nullable: false),
            LastName = table.Column<string>(maxLength: 50, nullable: false),
            DateOfBirth = table.Column<DateTime>(nullable: false),
            MailingAddress_Street1 = table.Column<string>(nullable: true),
            MailingAddress_Street2 = table.Column<string>(nullable: true),
            MailingAddress_City = table.Column<string>(nullable: true),
            MailingAddress_State = table.Column<string>(nullable: true),
            MailingAddress_Country = table.Column<string>(nullable: true),
            MailingAddress_ZipCode = table.Column<string>(nullable: true),
            MobilePhone = table.Column<string>(maxLength: 20, nullable: true),
            ProfileId = table.Column<int>(nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Person", x => x.UserId);
            table.ForeignKey(
                name: "FK_Person_Profile_ProfileId",
                column: x => x.ProfileId,
                principalTable: "Profile",
                principalColumn: "ProfileId",
                onDelete: ReferentialAction.Cascade);
        });

    migrationBuilder.CreateIndex(
        name: "IX_Person_ProfileId",
        table: "Person",
        column: "ProfileId",
        unique: true);
}
protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“个人资料”,
列:表=>new
{
ProfileId=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn),
Id=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_Profile”,x=>x.ProfileId);
});
migrationBuilder.CreateTable(
名称:“测试”,
列:表=>new
{
TestId=table.Column(可空:false)
.Annotation(“SqlServer:ValueGenerationStrategy”,SqlServerValueGenerationStrategy.IdentityColumn),
Id=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_检验”,x=>x.TestId);
});
migrationBuilder.CreateTable(
姓名:“人”,
列:表=>new
{
UserId=table.Column(可空:false),
Id=table.Column(可空:false),
FirstName=table.Column(maxLength:50,null:false),
LastName=table.Column(maxLength:50,nullable:false),
DateOfBirth=table.Column(可空:false),
MailingAddress_Street1=table.Column(可空:true),
MailingAddress_Street2=table.Column(可空:true),
MailingAddress_City=表.列(可空:true),
MailingAddress_State=table.Column(可空:true),
MailingAddress_Country=表.列(可空:true),
MailingAddress_ZipCode=table.Column(可空:true),
MobilePhone=table.Column(maxLength:20,nullable:true),
ProfileId=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_Person”,x=>x.UserId);
表1.外键(
名称:“FK_人员_档案_档案ID”,
列:x=>x.ProfileId,
原则性:“简介”,
principalColumn:“ProfileId”,
onDelete:引用。级联);
});
migrationBuilder.CreateIndex(
姓名:“IX_Person_ProfileId”,
表:“人”,
列:“ProfileId”,
独特:正确);
}

我希望
HasKey()
语法优先于Id字段和不创建的Id字段。

EF不会自动创建
Id
列(或FKs以外的任何其他列)。很可能这里没有显示的基本
实体
类包含类似
public int-Id{get;set;}
@IvanStoev的内容,就是这样!谢谢EF不会自动创建
Id
列(或FKs以外的任何其他列)。很可能这里没有显示的基本
实体
类包含类似
public int-Id{get;set;}
@IvanStoev的内容,就是这样!谢谢