C# 使用复合FKs时来自DB的EF Poco崩溃
下面是我的DB关系的样子 如果我使用现有数据库生成POCO类。VS 2015将生成以下类别 实体.csC# 使用复合FKs时来自DB的EF Poco崩溃,c#,entity-framework,C#,Entity Framework,下面是我的DB关系的样子 如果我使用现有数据库生成POCO类。VS 2015将生成以下类别 实体.cs [Table("Entity")] public partial class Entity { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Entity()
[Table("Entity")]
public partial class Entity
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Entity()
{
EntityOnlineInformation = new HashSet<EntityOnlineInformation>();
}
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EntityTypeId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EntityId { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EntityOnlineInformation> EntityOnlineInformation { get; set; }
}
[Table("EntityOnlineInformation")]
public partial class EntityOnlineInformation
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EntityId { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EntitytypeId { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int LanguageId { get; set; }
[Key]
[Column(Order = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int StoreId { get; set; }
public virtual Entity Entity { get; set; }
}
DBCOntext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity>()
.HasMany(e => e.EntityOnlineInformation)
.WithRequired(e => e.Entity)
.HasForeignKey(e => new { e.EntitytypeId, e.EntityId })
.WillCascadeOnDelete(false);
}
我没有对创建的poco类和dbcontext进行任何更改。我认为我的数据库关系可能是错误的,但使用edmx而不是POCO进行测试,一切正常。这里的问题是什么?我如何测试它?
如果我必须扩展OnModelCreating方法,请建议如何使用元数据在部分类中扩展它
编辑:我可以通过删除两个类中的虚拟属性和OnModelCreate方法中的流畅语法来解决问题。看起来对象之间的关系不起作用
谢谢这与您的答案没有多大关系,但您的帖子中似乎遗漏了一个属性<代码>EntityKeyword=新哈希集(),缺少
EntityKeyword
@stevenackley抱歉,它在那里,我只是没有添加整个类,因为有很多属性。我只添加了相关部分。我现在编辑了我的问题。
(473,10) : error 3015: Problem in mapping fragments starting at
lines 473, 498: Foreign key constraint 'Entity_EntityOnlineInformation'
from table EntityOnlineInformation (EntityId, EntitytypeId) to table Entity
(EntityTypeId, EntityId):: Insufficient mapping: Foreign key must be mapped to some
AssociationSet or EntitySets participating in a foreign key association on the conceptual side.