Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对EntityFrameworkCore中的关系感到困惑_C#_Asp.net_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# 对EntityFrameworkCore中的关系感到困惑

C# 对EntityFrameworkCore中的关系感到困惑,c#,asp.net,asp.net-core,entity-framework-core,C#,Asp.net,Asp.net Core,Entity Framework Core,目前我正在创建一个基于网络的游戏,我需要在EF中的关系帮助。我先用代码做表格,但我弄糊涂了。问题是: 如何使一个玩家拥有不同类型的武器?他们之间的关系是什么类型的? 在玩家模型中,我有i收集,但在武器模型中我该怎么做?如何建立关系,因为同一件武器可以被多个用户使用,而每个用户可以拥有多件武器?在武器模型中存储一名玩家,这将显示哪个用户当前正在使用该武器(或者如果同一件武器可以被多个玩家同时使用,则在武器模型中存储一组玩家).在武器模型中存储玩家,这将显示当前使用该武器的用户(或者如果同一武器可由

目前我正在创建一个基于网络的游戏,我需要在EF中的关系帮助。我先用代码做表格,但我弄糊涂了。问题是: 如何使一个
玩家
拥有不同类型的
武器
?他们之间的关系是什么类型的?
在玩家模型中,我有
i收集
,但在武器模型中我该怎么做?如何建立关系,因为同一件武器可以被多个用户使用,而每个用户可以拥有多件武器?

在武器模型中存储一名玩家,这将显示哪个用户当前正在使用该武器(或者如果同一件武器可以被多个玩家同时使用,则在武器模型中存储一组玩家).

在武器模型中存储玩家,这将显示当前使用该武器的用户(或者如果同一武器可由多个玩家同时使用,则在武器模型中存储一组玩家)。

您的意思是“多对多”关系,如下所示:

public class Phone
{
    public int Id { get; set; }
    public string Name { get; set; }
     
    public ICollection<Company> Companies { get; set; } // Many companies can develop this phone
 
    public Phone()
    {
        Companies = new List<Company>();
    }
}
 
public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
 
    public ICollection<Phone> Phones { get; set; } // The company can develop many phones
 
    public Company()
    {
        Phones = new List<Phone>();
    }
}
公共类电话
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection公司{get;set;}//许多公司都可以开发这款手机
公用电话
{
公司=新列表();
}
}
公营公司
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共i收集电话{get;set;}//该公司可以开发多种电话
上市公司()
{
电话=新列表();
}
}
您的意思是“多对多”关系,如下所示:

public class Phone
{
    public int Id { get; set; }
    public string Name { get; set; }
     
    public ICollection<Company> Companies { get; set; } // Many companies can develop this phone
 
    public Phone()
    {
        Companies = new List<Company>();
    }
}
 
public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
 
    public ICollection<Phone> Phones { get; set; } // The company can develop many phones
 
    public Company()
    {
        Phones = new List<Phone>();
    }
}
公共类电话
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection公司{get;set;}//许多公司都可以开发这款手机
公用电话
{
公司=新列表();
}
}
公营公司
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共i收集电话{get;set;}//该公司可以开发多种电话
上市公司()
{
电话=新列表();
}
}
使用intermadia表格

public class Weapon
{
  public int Id { get; set; }
  public ICollection<PlayerWeapon> PlayerWeapons{ get; set; }
}
public class Player
{
  public int Id { get; set; }
  public ICollection<PlayerWeapon> PlayerWeapons{ get; set; }
}
public class PlayerWeapon
{
 public int Id { get; set; }
 public int PlayerId { get; set; }
 public int WeaponId{ get; set; }

 [ForeignKey("PlayerId")]
 public Player Player{ get; set; }

[ForeignKey("WeaponId")]
public Weapon Weapon { get; set; }
}
公共级武器
{
公共int Id{get;set;}
公共ICollection播放器武器{get;set;}
}
公开课选手
{
公共int Id{get;set;}
公共ICollection播放器武器{get;set;}
}
公共类游戏武器
{
公共int Id{get;set;}
public int PlayerId{get;set;}
公共int武器ID{get;set;}
[外键(“玩家ID”)]
公共播放器播放器{get;set;}
[外键(“武器”)]
公共武器{get;set;}
}
使用intermadia表格

public class Weapon
{
  public int Id { get; set; }
  public ICollection<PlayerWeapon> PlayerWeapons{ get; set; }
}
public class Player
{
  public int Id { get; set; }
  public ICollection<PlayerWeapon> PlayerWeapons{ get; set; }
}
public class PlayerWeapon
{
 public int Id { get; set; }
 public int PlayerId { get; set; }
 public int WeaponId{ get; set; }

 [ForeignKey("PlayerId")]
 public Player Player{ get; set; }

[ForeignKey("WeaponId")]
public Weapon Weapon { get; set; }
}
公共级武器
{
公共int Id{get;set;}
公共ICollection播放器武器{get;set;}
}
公开课选手
{
公共int Id{get;set;}
公共ICollection播放器武器{get;set;}
}
公共类游戏武器
{
公共int Id{get;set;}
public int PlayerId{get;set;}
公共int武器ID{get;set;}
[外键(“玩家ID”)]
公共播放器播放器{get;set;}
[外键(“武器”)]
公共武器{get;set;}
}

在EF Core中,对于如何配置多对多关系,您应该包括一个实体(PlayerWeapon)来表示联接表,然后将导航属性添加到指向联接实体的多对多关系的任一侧

public class Player
{
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public ICollection<PlayerWeapon> PlayerWeapons { get; set; }
}

public class PlayerWeapon
{
    public int Id { get; set; }
    public int PlayerId { get; set; }
    public Player Player { get; set; }
    public int WeaponId { get; set; }
    public Weapon Weapon { get; set; }
}

public class Weapon
{
    public int Id { get; set; }
    public string WeaponName { get; set; }
    public ICollection<PlayerWeapon> PlayerWeapons { get; set; }
}
公共类播放器
{
公共int Id{get;set;}
公共字符串播放器名称{get;set;}
公共ICollection播放器武器{get;set;}
}
公共类游戏武器
{
公共int Id{get;set;}
public int PlayerId{get;set;}
公共播放器播放器{get;set;}
公共int武器ID{get;set;}
公共武器{get;set;}
}
公营武器
{
公共int Id{get;set;}
公共字符串WeaponName{get;set;}
公共ICollection播放器武器{get;set;}
}

有关更多详细信息,请参阅。

在EF Core中,有关如何配置多对多关系,应包括一个实体(PlayerWeapon)来表示联接表,然后将导航属性添加到指向联接实体的多对多关系的任一侧

public class Player
{
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public ICollection<PlayerWeapon> PlayerWeapons { get; set; }
}

public class PlayerWeapon
{
    public int Id { get; set; }
    public int PlayerId { get; set; }
    public Player Player { get; set; }
    public int WeaponId { get; set; }
    public Weapon Weapon { get; set; }
}

public class Weapon
{
    public int Id { get; set; }
    public string WeaponName { get; set; }
    public ICollection<PlayerWeapon> PlayerWeapons { get; set; }
}
公共类播放器
{
公共int Id{get;set;}
公共字符串播放器名称{get;set;}
公共ICollection播放器武器{get;set;}
}
公共类游戏武器
{
公共int Id{get;set;}
public int PlayerId{get;set;}
公共播放器播放器{get;set;}
公共int武器ID{get;set;}
公共武器{get;set;}
}
公营武器
{
公共int Id{get;set;}
公共字符串WeaponName{get;set;}
公共ICollection播放器武器{get;set;}
}
有关更多详细信息,请参阅。

我假设“许多玩家拥有许多武器”在这种情况下成立,因此,如前所述,您需要建模多对多关系,这需要添加第三个实体

公共密封类播放器
{
私有只读ICollection playerWeapons=new HashSet();
公共播放器(字符串名称,…)
{
名称=名称;
...
}
私人玩家()
{
}
公共Guid Id{get;private set;}
公共字符串名称{get;private set;}
public IEnumerable=>playerWeapons.Select(pw=>pw.weaple);
}
公共密封类武器
{
私有只读ICollection playerWeapons=new HashSet();
公共武器(字符串名称,…)
{
名称=名称;
...
}
私人武器()
{
}
公共Guid Id{get;private set;}
公共字符串名称{get;private set;}
public IEnumerable Players=>playerWeapons.Select(pw=>pw.Player);
}
公共密封类播放武器
{
公共玩家武器(玩家、武器)
{
玩家=玩家;
武器=武器;
PlayerId=Player.Id;
武器Id=武器.Id;
}
私人玩家武器()