C# 迁移一对多关系EF代码第一步
我正在使用LocalDb中的EF 6.1.3处理一个个人项目,使用code first aproach,但在使用PM Console迁移时出现了以下错误:C# 迁移一对多关系EF代码第一步,c#,ef-code-first,entity-framework-6,C#,Ef Code First,Entity Framework 6,我正在使用LocalDb中的EF 6.1.3处理一个个人项目,使用code first aproach,但在使用PM Console迁移时出现了以下错误: Post_Comments_Source_Post_Comments_Target: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical. 我试图在Post和Comment
Post_Comments_Source_Post_Comments_Target: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
我试图在Post和Comment类之间建立一对多的关系
以下是博士后课程:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string ShortDescription { get; set; }
public string Content { get; set; }
public DateTime PostedOn { get; set; }
public DateTime? Modified { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
后期映射:
public PostMap()
{
HasKey(t => t.PostId);
Property(t => t.PostId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.Title).IsRequired();
Property(t => t.ShortDescription);
Property(t => t.Content).IsRequired();
Property(t => t.PostedOn).IsRequired();
Property(t => t.Modified);
ToTable("Post");
}
注释映射
public class CommentMap : EntityTypeConfiguration<Comment>
{
public CommentMap()
{
HasKey(t => new { t.CommentId, t.PostId });
Property(t => t.CommentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.Title).IsRequired();
Property(t => t.Content).IsRequired();
ToTable("Comment");
HasRequired(t => t.Post).WithMany(c => c.Comments).HasForeignKey(t => t.PostId).WillCascadeOnDelete(false);
}
}
公共类CommentMap:EntityTypeConfiguration
{
公共地图()
{
HasKey(t=>new{t.CommentId,t.PostId});
属性(t=>t.CommentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
属性(t=>t.Title).IsRequired();
属性(t=>t.Content).IsRequired();
可转载(“评论”);
HasRequired(t=>t.Post)。有许多(c=>c.Comments)。HasForeignKey(t=>t.PostId)。WillCascadeOnDelete(false);
}
}
我有另外两个一对多关系和一个多对多关系,但似乎它们都有效,所以我不知道问题出在哪里
感谢您的帮助。这是您的问题:密钥由2个属性组成 HasKey(t=>new{t.CommentId,t.PostId}) 关系只有一个: HasRequired(t=>t.Post)。有许多(c=>c.Comments)。HasForeignKey(t=> t、 posted)。WillCascadeOnDelete(false)
检查此项:这是您的问题:密钥由2个属性组成 HasKey(t=>new{t.CommentId,t.PostId}) 关系只有一个: HasRequired(t=>t.Post)。有许多(c=>c.Comments)。HasForeignKey(t=> t、 posted)。WillCascadeOnDelete(false)
检查这个:我能看到的唯一东西是
HasKey(t=>new{t.CommentId,t.PostId})
在CommentMap上,我认为它应该只有一个键,CommentId
我同意bgs264。Comment类的复合主键是不必要的。它只需要一个键字段CommentId。我能看到的唯一东西是HasKey(t=>new{t.CommentId,t.PostId})
在CommentMap上,我认为它应该只有一个键,CommentId
我同意bgs264。Comment类的复合主键是不必要的。它只需要一个键字段CommentId。我能看到的唯一东西是HasKey(t=>new{t.CommentId,t.PostId})
在CommentMap上,我认为它应该只有一个键,CommentId
我同意bgs264。Comment类的复合主键不是必需的。它只需要一个键字段CommentId。
public class CommentMap : EntityTypeConfiguration<Comment>
{
public CommentMap()
{
HasKey(t => new { t.CommentId, t.PostId });
Property(t => t.CommentId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.Title).IsRequired();
Property(t => t.Content).IsRequired();
ToTable("Comment");
HasRequired(t => t.Post).WithMany(c => c.Comments).HasForeignKey(t => t.PostId).WillCascadeOnDelete(false);
}
}