在实体框架中映射与定义的中间实体的多对多关系5 [英] Mapping Many-to-Many Relationship With Defined Middle Entity in Entity Framework 5
问题描述
我正在试图明确定义多对多关系。明确地说,我的意思是我正在定义中间实体并使用Fluent API进行配置。以下是我的代码:
I'm trying to define a many-to-many relationship explicitly. By explicitly, I mean that I'm defining the middle entity and configuring it using the Fluent API. Below is my code:
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
表。一个是可空的外键,另一个是不可空的。我只想要生成后者,我不知道可以从哪里来的。
For some reason, two foreign keys are being created on the resultant ContentTypeColumn
table. One is a nullable foreign key, the other is non-nullable. I want only the latter to be generated, and I have no idea where the nullable key is coming from.
任何想法?
推荐答案
这是错误的:
this.HasRequired(x => x.ContentType)
.WithMany()
.HasForeignKey(x => x.ContentTypeId);
你有反向导航属性,所以你必须在 WithMany
或EF可能会创建两个关系:
You have reverse navigation property so you must use int in WithMany
or EF will probably create two relations:
this.HasRequired(x => x.ContentType)
.WithMany(y => y.ContentTypeColumns)
.HasForeignKey(x => x.ContentTypeId);
Btw。这种映射根本不需要,因为它是通过默认约定自动发现的。
Btw. this mapping should not be needed at all because it is discovered automatically through default conventions.
这篇关于在实体框架中映射与定义的中间实体的多对多关系5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!