微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 实体类型处于“阴影状态”是什么意思?

在我的ASP.NET Core 1.0,MVC6,EF7 Web应用程序中,我添加一个迁移,添加一个新的相关表(和相应的模型).我有以下模型快照:

[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
    protected override void BuildModel(ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasAnnotation("ProductVersion","7.0.0-rc1-16348")
            .HasAnnotation("sqlServer:ValueGenerationStrategy",sqlServerValueGenerationStrategy.IdentityColumn);

        modelBuilder.Entity("Salesboost.Models.ApplicationUser",b =>
        {
            b.Property<string>("Id");
            b.Property<int?>("TeamId");
            b.HasKey("Id");
            // -- <unrelated fields snipped> --
        });

        // -- <snipped> --

        modelBuilder.Entity("Team",b =>
        {
            b.Property<int>("Id").ValueGeneratedOnAdd();
            b.Property<string>("Name").Isrequired();
            b.Property<string>("ManagerId").Isrequired();
            b.HasKey("Id");
        });

        modelBuilder.Entity("Team",b =>
        {
            b.HasOne("ApplicationUser","Manager")
                .WithOne("TeamManaging")
                .HasForeignKey("ManagerId");
        });
    }
}

Team.cs:

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ManagerId { get; set; }

    public virtual ApplicationUser Manager { get; set; }
    public virtual ICollection<ApplicationUser> Members { get; set; }
}

ApplicationUser:

public class ApplicationUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser
{
    public int? TeamId { get; set; }

    public virtual Team Team { get; set; }
    public virtual Team TeamManaging { get; set; }
}

当我尝试更新数据库时,dnx给出了以下错误

The navigation property ‘Manager’ cannot be added to the entity type ‘Team’ because the entity type is defined in shadow state and navigations properties cannot be added to shadow state.

实体类型处于“阴影状态”意味着什么?有没有解决的办法?

解决方法

EF documentation解释了影子属性是什么:

You can use the Fluent API to configure shadow properties. Once you have called the string overload of Property – A.C. you can chain any of the configuration calls you would for other properties.

If the name supplied to the Property method (Property<...>("...") – A.C.) matches the name of an existing property – A.C. (a shadow property or one defined on the entity class),then the code will configure that existing property rather than introducing a new shadow property.

所以,当实体具有至少一个阴影属性时,我猜一个实体处于阴影状态.

这意味着在使用Property< ...>(“…”)的字符串重载时应该非常小心,因为这可能会引入阴影属性,即使您不需要它们也是如此.因此,当需要创建数据库时,EF会抱怨处于阴影状态的实体不存在CLR类型.

使用nameof()而不是普通字符串可能会有所帮助.因此,重载看起来像Property< ...>(nameof(…))更安全.

最后,为了更接近点,引入阴影属性来处理实体之间的关系.
以下说明如下:

By convention,shadow properties are only created when a relationship is discovered but no foreign key property is found in the dependent entity class. In this case,a shadow foreign key property will be introduced.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐