Sam*_*ran 3 c# fluent code-first ef-code-first
我有这门课:
public class Comment
{
public virtual int Id { get; set; }
public string Body { get; set; }
public DateTime AddDate { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
public int PostId { get; set; }
public virtual bool IsApproved { get; set; }
public virtual int LikeCount { get; set; }
public int? ParentId { get; set; }
public virtual Comment Parent { get; set; }
public ICollection<Comment> Children { get; set; }
public virtual Member Member { get; set; }
public virtual byte[] RowVersion { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我使用流畅的API,如下所示:
this.HasRequired(x => x.Post)
.WithMany(x => x.Comments)
.WillCascadeOnDelete();
this.HasOptional(x => x.Parent)
.WithMany(x=>x.Children)
.HasForeignKey(x=>x.ParentId)
.WillCascadeOnDelete(false);
Run Code Online (Sandbox Code Playgroud)
当我想运行该项目时,我收到此错误:
为表'Comments'指定了多个标识列.每个表只允许一个标识列.Visual Studio 2013
我在这个类中只有一个主键,它Id位于第一行.
任何人?我需要帮助
有时EF无法正确排序DbMigration类中的方法.当您重命名某些实体的PK然后添加迁移并尝试更新数据库时,可能会发生这种情况.为了使其工作,请转到您的迁移类(继承自DbMigration并在添加新迁移时自动生成的类),并在Up()方法中使所有drop语句位于所有其他语句之前.
public override void Up()
{
DropForeignKey();
DropForeignKey();
DropIndex();
DropPrimaryKey();
RenameColumn();
RenameColumn();
RenameIndex();
RenameIndex();
AddForeignKey();
AddForeignKey();
AddPrimaryKey();
}
Run Code Online (Sandbox Code Playgroud)
如果表的AddPrimaryKey()语句在DropPrimaryKey()语句之前,则在删除另一个主键之前添加主键,因此您将临时拥有该表的两个主键,并且主键是标识列.因此,您将获得"为表指定的多个标识列"异常.
| 归档时间: |
|
| 查看次数: |
6922 次 |
| 最近记录: |