【问题标题】:Entity Framework Code First - map same entity twice for different purposeEntity Framework Code First - 为不同的目的两次映射相同的实体
【发布时间】:2015-02-24 05:22:06
【问题描述】:

我有两个模型,一个 ApplicationUser 包含系统中的所有用户,我有一个报价模型,它将包含所有 Quotations 。现在我想在Quotations 中存储两个到ApplicationUser 的映射。这样我就可以映射到创建的用户以及取消的用户。我的模型是这样的

    public class Quotation
{
    public int QuotationID { get; set; }
    public DateTime QuotationDate { get; set; }
    public DateTime QuotationCancelDate { get; set; }
    public int ApplicationUserID { get; set; }
    public virtual ApplicationUser CreatedUser { get; set; }
    [ForeignKey("ApplicationUserID")]
    public ApplicationUser CancelledUser { get; set; }

}

但这会引发错误

Quotation_CancelledUser_Target_Quotation_CancelledUser_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ApplicationUserID' on entity 'Quotation' does not match the type of property 'Id' on entity 'ApplicationUser' in the referential constraint 'Quotation_CancelledUser'.

所以我想,我采取的方法是错误的。谁能指出实现这一目标的正确方法?

【问题讨论】:

  • 尝试将 ApplicationUserID 更改为 ApplicationUserId 吗?在属性和外键属性中。

标签: asp.net-mvc entity-framework ef-code-first code-first


【解决方案1】:

您观察到的问题称为“多级联路径”。当级联路径从表 A 中的列 col1 到表 B 以及从表 A 中的列 col2 到表 B 时,就会发生多级联路径。

当代码首次尝试添加列在另一个表中出现多次的表时,该异常是由 SQL Server 引起的。

在 SQL Server 中,一个表不能多次出现在由 DELETE 或 UPDATE 语句启动的所有级联引用操作的列表中。例如,级联引用操作树必须只有一个路径到级联引用操作树上的特定表。

您需要使用 FluentAPI 来配置关系。我目前使用的是EF5,不知道EF6/7能否实现。

所以修改你的代码示例,它看起来像:

public class Quotation
{
    public int QuotationID { get; set; }
    public DateTime QuotationDate { get; set; }
    public DateTime QuotationCancelDate { get; set; }

    public int CreatedUserID { get; set; }
    // Navigation property
    public virtual ApplicationUser CreatedUser { get; set; }

    public int CancelledUserID { get; set; }
    // Navigation property
    public virtual ApplicationUser CancelledUser { get; set; }
}

// Created a simple class for example
public class ApplicationUser
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
}

现在您可以在上下文类中编写:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //  Disable the default PluralizingTableNameConvention
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        // Add configuration here

        modelBuilder.Entity<Quotation>()
            .HasKey(e => e.QuotationID);

        modelBuilder.Entity<ApplicationUser>()
            .HasKey(e => e.Id);

        modelBuilder.Entity<Quotation>()
                        .HasRequired(a => a.CreatedUser)
                        .WithMany()
                        .HasForeignKey(u => u.CreatedUserID);

        modelBuilder.Entity<Quotation>()
            .HasRequired(a => a.CancelledUser)
            .WithMany()
            .HasForeignKey(u => u.CancelledUserID);
    }

有关示例的更多信息,请参阅link。

【讨论】:

  • 现在又报错了。Quotation_CancelledUser_Target_Quotation_CancelledUser_Source: : 一个引用约束的Dependent Role中所有属性的类型必须和Principal Role中对应的属性类型相同。实体 'Quotation' 上的属性 'CancelledUserID' 的类型与引用约束 'Quotation_CancelledUser' 中实体 'ApplicationUser' 上的属性 'Id' 的类型不匹配。 Quotation_CreatedUser_Target_Quotation_CreatedUser_Source: :
  • 引用约束的Dependent Role中所有属性的类型必须与Principal Role中对应的属性类型相同。实体 'Quotation' 上的属性 'CreatedUserID' 的类型与引用约束 'Quotation_CreatedUser' 中实体 'ApplicationUser' 上的属性类型 'Id' 不匹配。
  • 使用.HasForeignKey(u => u.CreatedUserID).WillCascadeOnDelete(false);
  • @Athul - EF 版本?我正在使用 EF5,上面的代码运行良好。
  • Athul,我刚刚查过了。上面的代码也适用于 EF6,没有任何变化。我认为由于设置中的密钥配置不正确,您遇到了错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2014-11-11
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多