gpt4 book ai didi

mysql - mysql中的EF自引用表

转载 作者:行者123 更新时间:2023-11-30 21:43:57 25 4
gpt4 key购买 nike

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

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 _db.Categories 获得类别时; Parent 属性和 Children 列表始终为 null,即使我添加了 include 子句。

PS:ParentId为父类时idCategory的值。

有人能告诉我我的代码和配置有什么问题吗?或者这只能在 SQLServer 中得到,而不能在 MySQL 中起作用。

最佳答案

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

模型(与你的相同,但我添加了 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 个 child )。

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


}

关于mysql - mysql中的EF自引用表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50342613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com