c# - EF 迁移异常 : A dependent property in a ReferentialConstraint is mapped to a store-generated column

标签 c# entity-framework asp.net-mvc-4 entity-framework-migrations

我已经查看了关于 SO 上有关此异常的现有问题,但似乎没有一个适用于我的情况。

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'SchemaDictionaryId'.

在程序包管理器控制台中执行 Update-Database 时出现此异常。

我的种子方法如下:

protected override void Seed(DataEntry.App_Data.DataEntryDataContext context)
{
    context.Schemas.AddOrUpdate(s => s.SchemaId, _generateSchemaData());
    context.SaveChanges();
}

private Schema[] _generateSchemaData()
{
    List<Schema> schemas = new List<Schema>();
    // Loop removed for simplicity
    schemas.Add(new Schema
    {
        // various property assignments
        SchemaDictionary = new SchemaDictionary
        {
            // various property assignments
        }
    });
    return schemas.ToArray();
}

我的 DbContext 子类包含以下流畅的 API 定义(针对相关性进行了修剪):

modelBuilder.Entity<Schema>()
    .HasKey(s => s.SchemaId);

modelBuilder.Entity<Schema>()
    .Property(s => s.SchemaId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();

modelBuilder.Entity<SchemaDictionary>()
    .HasKey(sd => sd.SchemaDictionaryId);

modelBuilder.Entity<SchemaDictionary>()
    .Property(s => s.SchemaDictionaryId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();

modelBuilder.Entity<SchemaDictionary>()
    .HasRequired(sd => sd.Schema)
    .WithOptional(s => s.SchemaDictionary);

最后是有问题的 POCO:

public class Schema
{
    public Guid SchemaId { get; set; }

    public Nullable<Guid> SchemaDictionaryId { get; set; }
    public virtual SchemaDictionary SchemaDictionary { get; set; }

    // various other properties
}

public class SchemaDictionary
{
    public Guid SchemaDictionaryId { get; set; }

    public Nullable<Guid> SchemaId { get; set; }
    public virtual Schema Schema { get; set; }

   // various other properties
}

SchemaSchemaDictionary 之间的关系是一对零或一...

  • Schema 对象将有零个或一个 SchemaDictionary
  • SchemaDictionary 将始终有一个 Schema

我对这个问题的理解是它不能支持 FK 约束,因为 EF 不知道 GUID 外键。定义了这样的依赖关系,如何插入种子数据?

最佳答案

当你这样做时:

modelBuilder.Entity<SchemaDictionary>()
    .HasRequired(sd => sd.Schema)
    .WithOptional(s => s.SchemaDictionary);

你是Configuring a Required-to-Optional Relationship (One-to–Zero-or-One) .您正在指定 SchemaDictionary 是依赖项,Schema 是主体

当您在 Entity Framework 中具有一对一关系时,从属关系中的 PK 也必须是委托(delegate)人的 FK。但是您已经指定它应该是数据库生成的。该列不能既是 FK 又是 IDENTITY,因此您会收到错误消息。您应该从 SchemaDictionaryId 属性中删除 HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)

顺便说一句,你的一些代码是不必要的

  1. HasKey 因为 EF 将假定 SchemaIdSchemaDictionaryID 是命名约定的键

  2. IsRequired() 在不可为 null 的字段上

引用资料:

When mapping, which is end is the principal?

What does principal end mean?

关于c# - EF 迁移异常 : A dependent property in a ReferentialConstraint is mapped to a store-generated column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19793572/

相关文章:

c# - 在 MVC4 中使用 @Html.RenderAction 不显示页面

c# - AutoMapper - 如何使用 ConstructedBy 方法将参数传递给自定义解析器?

wpf - 使用 WPF/Entity Framework 跨多个窗口进行数据绑定(bind)?是否传递上下文?

c# - LINQ Lambda,使用列表分组

c# - 具有 ASP.NET 标识的 Entity Framework Multi-Tenancy

c# - 将 viewbag 从 Action Controller 传递到局部 View

c# - 如何在 Web API 中实现 HttpMessageHandler?

c# - 实现无锁队列(用于 Logger 组件)

c# - 如何防止在类准备就绪之前调用该类的方法?

c# - 刚体圆周运动