为什么这个EF 6代码会产生意外的代码优先迁移结果? 我希望这段代码只生成一个自引用FK的ParentId。 这是基于最后附带的基于EF 4.1的文章。在这种情况下,EF 6中是否有任何可能影响行为的变化?
实体:
% Read the image into the workspace and display it.
A = imread('coins.png');
imshow(A)
% Find all the circles with radius r such that 15 ≤ r ≤ 30.
[centers, radii, metric] = imfindcircles(A,[15 30]);
% Retain the five strongest circles according to the metric values.
centersStrong5 = centers(1:5,:);
radiiStrong5 = radii(1:5);
metricStrong5 = metric(1:5);
% Draw the five strongest circle perimeters.
viscircles(centersStrong5, radiiStrong5,'EdgeColor','b');
代码优先迁移结果:
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Children { get; set; }
}
public class CategoryMap : EntityTypeConfiguration<Category> {
public CategoryMap() {
HasKey(x => x.CategoryId);
Property(x => x.CategoryId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.Name)
.IsRequired()
.HasMaxLength(255);
HasOptional(x => x.Parent)
.WithMany(x => x.Children)
.HasForeignKey(x => x.ParentId)
.WillCascadeOnDelete(false);
}
}
代码优先迁移期望:
CreateTable(
"dbo.Categories",
c => new
{
CategoryId = c.Int(nullable: false, identity: true),
Name = c.String(),
ParentId = c.Int(),
Parent_CategoryId = c.Int(),
})
.PrimaryKey(t => t.CategoryId)
.ForeignKey("dbo.Categories", t => t.Parent_CategoryId)
.Index(t => t.Parent_CategoryId);
您可以将此作为参考,以获取更多详细信息:http://dotnet.dzone.com/news/using-self-referencing-tables