EF Core多对零关系配置

时间:2016-10-29 15:47:06

标签: entity-framework-core

如何防止EF Core迁移向ShadowZone添加shadow属性:

public partial class InitialCreate : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Core_TimeZone",
            schema: "dbo",
            columns: table => new
            {
                StateOrProvinceId = table.Column<int>(nullable: true),
                ...
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Core_TimeZone", x => x.Id);
                table.ForeignKey(
                    name: "FK_Core_TimeZone_Core_StateOrProvince_StateOrProvinceId",
                    column: x => x.StateOrProvinceId,
                    principalSchema: "dbo",
                    principalTable: "Core_StateOrProvince",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });
    }
}

以下是POCO课程:

public partial class TimeZone : BaseEntity
{
    …
   // no StateOrProvince related properties
}


public partial class StateOrProvince : BaseEntity
{
    …
    private ICollection<Dna.NetCore.Core.BLL.Entities.Common.TimeZone> _timeZones;
    public virtual ICollection<Dna.NetCore.Core.BLL.Entities.Common.TimeZone> TimeZones
    {
        get { return _timeZones ?? (_timeZones = new List<Dna.NetCore.Core.BLL.Entities.Common.TimeZone>()); }
        set { _timeZones = value; }
    }
}

和配置类:

public class StateOrProvinceConfiguration : IEntityTypeConfiguration<StateOrProvince>
{
    public void Map(EntityTypeBuilder<StateOrProvince> builder)
    {
        …
        builder.HasMany(d => d.TimeZones)
               .WithOne()
               .OnDelete(DeleteBehavior.Restrict);
    }
}

无论是否包含HasMany()。WithOne()配置,我都会获得shadow属性。

完整source code is located in this GitHub repository

0 个答案:

没有答案