Entity Framework 6:多对多关系问题

huangapple go评论65阅读模式
英文:

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&lt;Samurai&gt; Samurais = new List&lt;Samurai&gt;();
    }
}

and

namespace SamuraiApp.Domain
{
    public class Samurai
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List&lt;Quote&gt; Quotes { get; set; } = new List&lt;Quote&gt;();
        public List&lt;Battle&gt; Battles { get; set; } = new List&lt;Battle&gt;();
    }
}

and dbcontext

 public class SamuraiContext:DbContext
    {
        public DbSet&lt;Samurai&gt; Samurais { get; set; }
        public DbSet&lt;Quote&gt; Quotes { get; set; }
        public DbSet&lt;Battle&gt; Battles { get; set; }
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(&quot;Data Source= (localdb)\\MSSQLLocalDB; Initial Catalog=SamuraiAppData&quot;);
            // si on a sqlLite on va &#233;crire  optionsBuilder.UseSqlLite(&quot;&lt;chaine de connexion sqlite&gt;&quot;)

        }

    }

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&lt;Samurai&gt;();
        }
        public int BattleId { get; set; }
        public string Name { get; set; }
        public ICollection&lt;Samurai&gt; Samurais {get; set;}
    }
}

namespace SamuraiApp.Domain
{
    public class Samurai
    {
        public Samurai()
        {
           this.Battles = new HashSet&lt;Battle&gt;();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public List&lt;Quote&gt; Quotes { get; set; } = new List&lt;Quote&gt;();
        public ICollection&lt;Battle&gt; Battles { get; set; };
    }
}

huangapple
  • 本文由 发表于 2023年2月23日 19:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定