当前位置 : 主页 > 网络编程 > ASP >

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

来源:互联网 收集:自由互联 发布时间:2021-06-24
在我的ASP.NET Core 1.0,MVC6,EF7 Web应用程序中,我添加了一个迁移,添加了一个新的相关表(和相应的模型).我有以下模型快照: [DbContext(typeof(ApplicationDbContext))]partial class ApplicationDbContextModelSn
在我的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.

网友评论