实体类型处于“阴影状态"是什么意思? [英] What does it mean for an entity type to be in "shadow state"?

查看:119
本文介绍了实体类型处于“阴影状态"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my ASP.NET Core 1.0, MVC6, EF7 web application, I'm adding a migration that adds a new related table (& corresponding model). I have the following model snapshot:

[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:

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给我以下错误:

When I attempt to update the database, dnx gives me the following error:

导航属性'Manager'不能添加到实体类型'Team',因为实体类型是在阴影状态下定义的,并且导航属性不能添加到阴影状态.

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.

实体类型处于影子状态"是什么意思?有办法解决吗?

What does it mean for an entity type to be in "shadow state"? Is there a way around this?

推荐答案

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

The EF documentation explains what a shadow property is:

您可以使用Fluent API配置阴影属性.调用 Property的字符串重载-A.C.之后,您可以链接对其他属性的任何配置调用.

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.

如果提供给Property方法的名称(Property<...>("...")-AC)与现有属性的名称-AC(影子属性或实体类上定义的一个)匹配,则代码将配置该现有属性,而不是引入新的shadow属性.

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.

因此,我想当实体具有至少一个 shadow属性 时,该实体处于阴影状态.

So, I guess an entity is in shadow state when the entity is having at least one shadow property.

这意味着在使用Property<...>("...")的字符串重载时应该非常小心,因为即使您不需要阴影属性,这也会引入阴影属性.结果,当需要创建数据库时,EF抱怨影子状态下的实体不存在CLR类型.

This means that you should be very careful when using the string overload of Property<...>("..."), since this may introduce shadow properties even if you do not need them. As a result, when the database needs to be created EF complains that no CLR type exists for the entity in shadow state.

使用nameof()代替纯字符串可能会有所帮助.因此,重载看起来像Property<...>(nameof(...))那样更安全.

Using nameof() instead of plain strings may help. Thus the overload would look like Property<...>(nameof(...)) which is safer.

最后,为了更接近点,引入了阴影属性以处理实体之间的关系. 以下内容对此进行了解释:

And finally, to get closer to the point shadow properties are introduced to handle relationships between entities. The following explains it:

按照惯例,只有在发现关系但在从属实体类中找不到外键属性时,才创建阴影属性.在这种情况下,将引入影子外键属性.

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.

这篇关于实体类型处于“阴影状态"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆