在创建之前,foreach会达到ShadowProperties

时间:2018-10-19 08:48:13

标签: c# entity-framework entity-framework-core ef-core-2.1

我是Entity Framework的新手,并且正在使用Julie Lerman的Pluralsight课程学习atm。我正在看第二门课程“ Entity Framework Core 2: Mappings”,但是我正在使用EF Core 2.1。

编辑: 因此,我决定将所有内容都注释掉,然后再次按照本课程进行操作,现在它可以正常工作,但是生成的迁移不应该生成2列:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<DateTime>(
                name: "BetterName_Created",
                table: "Samurais",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

    migrationBuilder.AddColumn<string>(
                name: "GivenName",
                table: "Samurais",
                nullable: true);

    migrationBuilder.AddColumn<DateTime>(
                name: "BetterName_LastModified",
                table: "Samurais",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

    migrationBuilder.AddColumn<string>(
                name: "SurName",
                table: "Samurais",
                nullable: true);
}

SamuraiContext.cs

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<SamuraiBattle>().HasKey(s => new { s.SamuraiId, s.BattleId });

    modelBuilder.Entity<Battle>().Property(b => b.StartDate).HasColumnType("Date");
    modelBuilder.Entity<Battle>().Property(b => b.EndDate).HasColumnType("Date");

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        modelBuilder.Entity(entityType.Name).Property<DateTime>("Created");  
        modelBuilder.Entity(entityType.Name).Property<DateTime>("LastModified");
    }

    modelBuilder.Entity<Samurai>().OwnsOne(s => s.BetterName).Property(b => b.GivenName).HasColumnName("GivenName");
    modelBuilder.Entity<Samurai>().OwnsOne(s => s.BetterName).Property(b => b.SurName).HasColumnName("SurName");
}

在添加GivenName / Surname之前,已经构建了foreach上下文,直到一切都按预期工作为止。但是,在为列名添加了最后两行之后,为什么还要添加BetterName_Created和BetterName_LastModified? (根据课程设置)

PersonFullName.cs

public class PersonFullName
{
    public string SurName { get; set; }
    public string GivenName { get; set; }
    public string FullName => $"{GivenName} {SurName}";
    public string FullNameReverse => $"{SurName}, {GivenName}";

    public PersonFullName(string givenName, string surName)
    {
        SurName = surName;
        GivenName = givenName;
    }
}

Samurai.cs

public class Samurai
{
    public int Id { get; set; }
    public string Name { get; set; }
    public PersonFullName BetterName { get; set; }
    public List<Quote> Quotes { get; set; }
    public List<SamuraiBattle> SamuraiBattles { get; set; }
    public SecretIdentity SecretIdentity { get; set; }

    public Samurai()
    {
        Quotes = new List<Quote>();
        SamuraiBattles = new List<SamuraiBattle>();
    }
}

最好的问候, 阿德里亚诺。

1 个答案:

答案 0 :(得分:3)

这是因为foreach循环还为拥有的实体定义了阴影属性。请记住,按照EF核心术语,拥有的实体仍然是实体,因此GetEntityTypes()会将它们包括在结果集中。

EF Core提供了IsOwned扩展方法,可用于识别它们并进行特殊处理,或者在这种情况下,只需跳过它们即可:

foreach (var entityType in modelBuilder.Model.GetEntityTypes().Where(t => !t.IsOwned())
{
    // ...
}

此外,此类循环应在发现所有实体和拥有的实体类型之后进行。如果PersonFullName未被标记为[Owned]属性,请在foreach调用之后(或者最好在OwnsOne末尾)移动OnModelCreating。 / p>