mysql中的实体框架ef自引用表

7dl7o3gd  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(290)

我想要一个在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模型配置

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\u db.categories获取类别时;父属性和子列表总是空的,即使我添加了include子句。
ps:如果将类别设置为父类别,则parentid是idcategory值。
有人能告诉我我的代码和配置有什么问题吗?或者这是只能在sqlserver中获取而不能在mysql中获取工作。

c9x0cxw0

c9x0cxw01#

我认为你复制/粘贴并更改了代码,所以它不是你真正的应用程序。你的配置有点奇怪(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});
    }
}

model(与您的相同,但是我添加了children集合的初始化

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);
    }

}

相关问题