C# 如何在C中用一个外键将两个不同的表引用到一个公共表#
我有三个职业,道具,护甲和武器 ITEM是父类,具有通用属性,装甲和武器是我想要链接到ITEM类的特定属性 以下是数据库架构: 其中一项是护甲或武器,当我查询时,我需要属于该项和特定类的所有属性C# 如何在C中用一个外键将两个不同的表引用到一个公共表#,c#,mysql,database,.net-core,entity-framework-core,C#,Mysql,Database,.net Core,Entity Framework Core,我有三个职业,道具,护甲和武器 ITEM是父类,具有通用属性,装甲和武器是我想要链接到ITEM类的特定属性 以下是数据库架构: 其中一项是护甲或武器,当我查询时,我需要属于该项和特定类的所有属性 public class Item { public Guid Id { get; set; } public bool Consumable { get; set; } public string Description { get; set; } public st
public class Item
{
public Guid Id { get; set; }
public bool Consumable { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public float Weight { get; set; }
}
public class Armor
{
public Guid Id { get; set; }
public string ArmorClass { get; set; }
public ArmorType ArmorType { get; set; }
public bool Stealth { get; set; }
public int Strength { get; set; }
}
public class Weapon
{
public Guid Id { get; set; }
public int Damage { get; set; }
public WeaponType WeaponType { get; set; }
public List<string> Properties { get; set; }
}
公共类项目
{
公共Guid Id{get;set;}
公共bool消耗品{get;set;}
公共字符串说明{get;set;}
公共字符串名称{get;set;}
公开浮动价格{get;set;}
公共浮动权重{get;set;}
}
公共级装甲
{
公共Guid Id{get;set;}
公共字符串ArmorClass{get;set;}
公共ArmorType ArmorType{get;set;}
公共布尔隐形{get;set;}
公共整数强度{get;set;}
}
公营武器
{
公共Guid Id{get;set;}
公共整数损坏{get;set;}
公共WeaponType WeaponType{get;set;}
公共列表属性{get;set;}
}
我在如何链接这些表、在哪里写什么属性以及诸如此类的问题上遇到了麻烦
我们的想法是使用EF Core。您打算继承。EF TPT中有两种类型,每个类型的表(每个类型都有自己的表,它们共享主键)和每个继承的TPI表(整个继承都有一个表,并且有一个列来区分这些类型)。EF core只支持第二个。这意味着您的数据库模式需要更改为只有一个表用于此功能 您可以做的另一件事是切换到组合而不是继承。因此,武器和盔甲将由一件物品和额外属性组成 这里有一篇文章可以帮助您了解具体情况:
您要继承遗产。EF TPT中有两种类型,每个类型的表(每个类型都有自己的表,它们共享主键)和每个继承的TPI表(整个继承都有一个表,并且有一个列来区分这些类型)。EF core只支持第二个。这意味着您的数据库模式需要更改为只有一个表用于此功能 您可以做的另一件事是切换到组合而不是继承。因此,武器和盔甲将由一件物品和额外属性组成 这里有一篇文章可以帮助您了解具体情况:
选项1
如果您确实需要单独的
项目
表格你需要
装甲和武器类中的物品参考
public class Item
{
public Guid Id { get; set; }
public bool Consumable { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public float Weight { get; set; }
// Following reference properties will get the **Armor** or **Weapon** properties, but it allows an `Item` to be an `Armor` and a `Weapon` at the same time
public Armor Armor { get; set; }
public Weapon Weapon { get; set; }
}
public class Armor
{
public Guid Id { get; set; }
public Item Item { get; set; }
public string ArmorClass { get; set; }
public ArmorType ArmorType { get; set; }
public bool Stealth { get; set; }
public int Strength { get; set; }
}
public class Weapon
{
public Guid Id { get; set; }
public Item Item { get; set; }
public int Damage { get; set; }
public WeaponType WeaponType { get; set; }
public List<string> Properties { get; set; }
}
这样,当您查询任何装甲
或武器
时,您将获得所有属性
建议
您尝试使用的模型将您限制为只有两种类型的项s
如果您的系统将来可能会有更多类型的项目,或者您的项目类型具有其他属性,我建议您应该有以下表格
- 类别(记录为装甲和武器)
- 属性(记录将是隐形、强度、伤害、类型等)
- 类别属性(属性到类别的映射)
- 项目
- ItemCategoryAttributes(属性值)
选项1
如果您确实需要单独的项目
表格
你需要装甲和武器类中的物品参考
public class Item
{
public Guid Id { get; set; }
public bool Consumable { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public float Weight { get; set; }
// Following reference properties will get the **Armor** or **Weapon** properties, but it allows an `Item` to be an `Armor` and a `Weapon` at the same time
public Armor Armor { get; set; }
public Weapon Weapon { get; set; }
}
public class Armor
{
public Guid Id { get; set; }
public Item Item { get; set; }
public string ArmorClass { get; set; }
public ArmorType ArmorType { get; set; }
public bool Stealth { get; set; }
public int Strength { get; set; }
}
public class Weapon
{
public Guid Id { get; set; }
public Item Item { get; set; }
public int Damage { get; set; }
public WeaponType WeaponType { get; set; }
public List<string> Properties { get; set; }
}
这样,当您查询任何装甲
或武器
时,您将获得所有属性
建议
您尝试使用的模型将您限制为只有两种类型的项s
如果您的系统将来可能会有更多类型的项目,或者您的项目类型具有其他属性,我建议您应该有以下表格
- 类别(记录为装甲和武器)
- 属性(记录将是隐形、强度、伤害、类型等)
- 类别属性(属性到类别的映射)
- 项目
- ItemCategoryAttributes(属性值)
但像这样,我无法查询物品并获取装甲或武器属性,对吗?但像这样,我无法查询物品并获取装甲或武器属性,对吗?