【问题标题】:Mapping Many-to-Many Relationship With Defined Middle Entity in Entity Framework 5在 Entity Framework 5 中使用已定义的中间实体映射多对多关系
【发布时间】:2012-10-04 15:03:17
【问题描述】:

我正在尝试明确定义多对多关系。明确地说,我的意思是我正在定义中间实体并使用 Fluent API 对其进行配置。以下是我的代码:

public class ContentType
{
    public Int64 Id { get; set; }
    public Guid SID { get; set; }
    public String Name { get; set; }
    public ContentType Parent { get; set; }
    public Nullable<Int64> ParentId { get; set; }
    public Category Category { get; set; }
    public Nullable<Int64> CategoryId { get; set; }
    public Boolean IsLocked { get; set; }
    public virtual ICollection<ContentTypeColumn> ContentTypeColumns { get; set; }
}

public class Column
{
    public Int64 Id { get; set; }
    public Guid SID { get; set; }
    public String SchemaName { get; set; }
    public DataType DataType { get; set; }
    public Int32 DataTypeId { get; set; }
    public virtual ICollection<ContentTypeColumn> ContentTypeColumns { get; set; }
}

public class ContentTypeColumn
{
    public Int64 Id { get; set; }
    public Int64 ColumnId { get; set; }
    public Column Column { get; set; }
    public ContentType ContentType { get; set; }
    public Int64 ContentTypeId { get; set; }
    public Boolean IsRequired { get; set; }
    public Boolean IsCalculated { get; set; }
    public String Title { get; set; }
    public Boolean IsSystem { get; set; }
    public Expression Expression { get; set; }
    public Int32 ExpressionId { get; set; }
    public virtual ICollection<ColumnRule> Rules { get; set; }
}

public class ContentTypeConfiguration : EntityTypeConfiguration<ContentType>
{
    public ContentTypeConfiguration()
    {
        this.ToTable("ContentType");
        this.Property(x => x.Id).HasColumnName("ContentTypeId").IsRequired();

        this.Property(x => x.Name).HasMaxLength(30);
        this.HasOptional(x => x.Parent)
            .WithMany()
            .HasForeignKey(x => x.ParentId);

        this.Property(x => x.SID).IsRequired();
    }
}

public class ContentTypeColumnConfiguration : EntityTypeConfiguration<ContentTypeColumn>
{
    public ContentTypeColumnConfiguration()
    {
        this.ToTable("ContentTypeColumn");
        this.HasRequired(x => x.ContentType)
            .WithMany()
            .HasForeignKey(x => x.ContentTypeId);
        this.Property(x => x.Title).HasMaxLength(50).IsRequired();
        this.Property(x => x.Id).HasColumnName("ContentTypeColumnId");
    }
}

由于某种原因,正在生成的 ContentTypeColumn 表上创建了两个外键。一个是可为空的外键,另一个是不可为空的。我只想生成后者,我不知道可空键来自哪里。

有什么想法吗?

【问题讨论】:

    标签: entity-framework mapping entity-framework-5


    【解决方案1】:

    这是错误的:

    this.HasRequired(x => x.ContentType)
        .WithMany()
        .HasForeignKey(x => x.ContentTypeId);
    

    你有反向导航属性,所以你必须在 WithMany 中使用 int 否则 EF 可能会创建两个关系:

    this.HasRequired(x => x.ContentType)
        .WithMany(y => y.ContentTypeColumns)
        .HasForeignKey(x => x.ContentTypeId);
    

    顺便说一句。根本不需要此映射,因为它是通过默认约定自动发现的。

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      相关资源
      最近更新 更多