mysql中的EF自引用表

时间:2018-05-15 04:59:45

标签: mysql entity-framework entity-framework-6

我想在mysql(版本5.7.20)中使用EF(版本6.0)的一个自引用表。

MySQL表:

Create table Category
id int(11) Primary Key,
name varchar(50),
ParentId int(11) Default Null,
PRIMARY KEY (`id`),
KEY`Parent_Key` (`ParentId`),
CONSTRAINT`Parent_Key` FOREIGN KEY (`ParentId`) REFERENCES`Category` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

EF代码:

public class Category
{
    public int Id {get; set;}
    public string Description {get; set;}
    public int? ParentId {get; set;}

    public virtual Category Parent {get; set;}
    public virtual ICollection<Category> Children {get; set;}
};

EF ModelConfiguration

CategoryConfiguration() 
{
  this.HasKey(e => new {e.id})
  this.Property(e => e.id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
  this.HasOptional(e => e.Parent) .WithMany(e => Children) .HasForeignKey(e => new {e.id, e.ParentId); }

这是运行代码的骨架,它运行时没有错误。问题是当我通过linq _db.Categories获得Category时; Parent属性和Children列表始终为null,即使我添加了include子句。

PS:如果将类别设置为父类别,则ParentId是idCategory值。

有人会告诉我我的代码和配置有什么问题吗?或者这只能在SQLServer中获得无法在MySQL中工作。

1 个答案:

答案 0 :(得分:0)

我认为您复制/粘贴并更改了代码,因此它不是您的真实应用。您的配置中有一些奇怪的东西(2个键) 无论如何,这里有一个MySQL的工作示例

上下文

class Context : DbContext
{
    public Context(){}

    public Context(DbConnection connection) : base (connection, true){}

    public DbSet<Category> Categories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Category>()
            .HasOptional(e => e.Parent).WithMany(e => e.Children).HasForeignKey(e => new {e.ParentId});
    }
}

模型(与您相同,但我已经添加了儿童集合的初始化

public class Category
{
    public Category()
    {
        Children = new List<Category>();
    }

    public int Id {get; set;}

    [MaxLength(50)]
    public string Description {get; set;}

    public int? ParentId {get; set;}

    public virtual Category Parent {get; set;}
    public virtual ICollection<Category> Children {get; set;}
}

测试应用程序(如果您运行超过1次,您将收到异常,因为您将有6个孩子)。

static void Main(string[] args)
{

    string connectionString = "Server=10.0.0.26;Database=EfTest;Uid=admin;Pwd=password";


    using (DbConnection connection = new MySqlConnection(connectionString))
    using (Context context = new Context(connection))
    {
        context.Categories.Add(new Category() { Description = "Root" });
        context.SaveChanges();

        Category rootCategory = context.Categories.First(_ => _.Description == "Root");
        rootCategory.Children.Add(new Category() { Description = "Child1" });
        rootCategory.Children.Add(new Category() { Description = "Child2" });
        rootCategory.Children.Add(new Category() { Description = "Child3" });
        context.SaveChanges();
    }

    using (DbConnection connection = new MySqlConnection(connectionString))
    using (Context context = new Context(connection))
    {
        Category rootCategory = context.Categories.First(_ => _.Description == "Root");
        Debug.Assert(rootCategory.Children.Count == 3);
    }


}