英文:
Entity Framework 6: Problem with many to many relationship
问题
我从一个教程开始,其中创建了以下类:
namespace SamuraiApp.Domain
{
public class Battle
{
public int BattleId { get; set; }
public string Name { get; set; }
public List<Samurai> Samurais = new List<Samurai>();
}
}
和
namespace SamuraiApp.Domain
{
public class Samurai
{
public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; } = new List<Quote>();
public List<Battle> Battles { get; set; } = new List<Battle>();
}
}
以及 DbContext:
public class SamuraiContext : DbContext
{
public DbSet<Samurai> Samurais { get; set; }
public DbSet<Quote> Quotes { get; set; }
public DbSet<Battle> Battles { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SamuraiAppData");
// 如果使用 SQLite,将会写成 optionsBuilder.UseSqlLite("<SQLite connection string>");
}
}
我以为Entity Framework 6能够单独解释多对多关系并生成中间表BattleSamurai,但是基类的生成没有考虑到这种关系(参见截图)。
请问您有任何评论吗?
英文:
I started with a tutorial, where I created classes :
namespace SamuraiApp.Domain
{
public class Battle
{
public int BattleId { get; set; }
public string Name { get; set; }
public List<Samurai> Samurais = new List<Samurai>();
}
}
and
namespace SamuraiApp.Domain
{
public class Samurai
{
public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; } = new List<Quote>();
public List<Battle> Battles { get; set; } = new List<Battle>();
}
}
and dbcontext
public class SamuraiContext:DbContext
{
public DbSet<Samurai> Samurais { get; set; }
public DbSet<Quote> Quotes { get; set; }
public DbSet<Battle> Battles { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source= (localdb)\\MSSQLLocalDB; Initial Catalog=SamuraiAppData");
// si on a sqlLite on va écrire optionsBuilder.UseSqlLite("<chaine de connexion sqlite>")
}
}
I thought that entityFramework6 alone interprets the many-to-many relationships and generates the intermediate table BattleSamurai, but the generation of the base did not take into account this relationship (see screenshot).
Do you have any comments, please?
答案1
得分: 1
将public int BattleId { get; set; }
添加到Samurai
类,并将public int SamuraiId { get; set; }
添加到Battle
类。
确保在迁移中可以看到Samurai
类是否有对Battle
的外键关联,反之亦然。
英文:
Add public int BattleId { get; set; }
to Samurai
and public int SamuraiId { get; set; }
to Battle
.
To make sure you can see in migration if Samurai
has foreign key to Battle
and vice versa.
答案2
得分: 0
根据这篇文章,关于EF中的多对多关系,代码应该如下所示(请注意,我无法看到Quote
类,可能需要在Samurai
类中也进行重构以匹配ICollection
):
namespace SamuraiApp.Domain
{
public class Battle
{
public Battle()
{
this.Samurais = new HashSet<Samurai>();
}
public int BattleId { get; set; }
public string Name { get; set; }
public ICollection<Samurai> Samurais { get; set; }
}
}
namespace SamuraiApp.Domain
{
public class Samurai
{
public Samurai()
{
this.Battles = new HashSet<Battle>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; } = new List<Quote>();
public ICollection<Battle> Battles { get; set; }
}
}
英文:
I am following this article about Many-to-Many relationships in EF.
According to it, the code should look like this (Please note that I can't see the Quote class, probably, you have to refactor that one too at Samurai class to match ICollection):
namespace SamuraiApp.Domain
{
public class Battle
{
public Battle()
{
this.Samurais = new HashSet<Samurai>();
}
public int BattleId { get; set; }
public string Name { get; set; }
public ICollection<Samurai> Samurais {get; set;}
}
}
namespace SamuraiApp.Domain
{
public class Samurai
{
public Samurai()
{
this.Battles = new HashSet<Battle>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; } = new List<Quote>();
public ICollection<Battle> Battles { get; set; };
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论