Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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# 删除属性并继承它们-代码优先的方法_C#_Sql Server_Asp.net Core Webapi - Fatal编程技术网

C# 删除属性并继承它们-代码优先的方法

C# 删除属性并继承它们-代码优先的方法,c#,sql-server,asp.net-core-webapi,C#,Sql Server,Asp.net Core Webapi,所以,我一直在开发一个.NET核心Web API应用程序,用于体育成绩、团队、比赛、比赛。。。你明白了 现在,我在创建实体时没有想到的是,我将有不同类型的比赛-对于每一项运动,一场比赛将有几乎相同的属性,但有些将是特定于运动的。 篮球有四节,冰球有三节,足球只有两个半场。还有更多的例子说明了这些运动的比赛模式的不同之处 问题是我最初只创建了一个模型-比赛,这两支球队中的每支都有两个半场属性。 我想做的是:从Match模型中删除这两个属性,并使Match成为一个基类,它将被BasketballMa

所以,我一直在开发一个.NET核心Web API应用程序,用于体育成绩、团队、比赛、比赛。。。你明白了

现在,我在创建实体时没有想到的是,我将有不同类型的比赛-对于每一项运动,一场比赛将有几乎相同的属性,但有些将是特定于运动的。 篮球有四节,冰球有三节,足球只有两个半场。还有更多的例子说明了这些运动的比赛模式的不同之处

问题是我最初只创建了一个模型-比赛,这两支球队中的每支都有两个半场属性。 我想做的是:从Match模型中删除这两个属性,并使Match成为一个基类,它将被BasketballMatch、FootballMatch、IceHockeyMatch继承

有没有一种方法可以在不破坏数据库的情况下,使用代码优先的方法来重新构造这种结构?如果需要删除表并创建具有正确继承结构的新表,则可以删除所有匹配项

public class Match
{
    [Key]
    public int ID { get; set; }

    public DateTime Date { get; set; }

    [ForeignKey("Sport")]
    public int SportID { get; set; }

    public Sport Sport { get; set; }

    [ForeignKey("Competition")]
    public int CompetitionID { get; set; }

    public Competition Competition { get; set; }

    [ForeignKey("HomeTeam")]
    public int HomeTeamID { get; set; }

    public Team HomeTeam { get; set; }

    [ForeignKey("AwayTeam")]
    public int AwayTeamID { get; set; }

    public Team AwayTeam { get; set; }

    public int? HalfTimeHomeTeamScore { get; set; }

    public int? HalfTimeAwayTeamScore { get; set; }

    public int? HomeTeamScore { get; set; }

    public int? AwayTeamScore { get; set; }
}
这些:

是单独的实体,
MatchPart
s,如果您愿意,它们不属于匹配本身,而是应该引用它,原因有很多:

  • 每场比赛的比赛部分数量取决于运动类型,有时也取决于锦标赛(例如,男子网球专业选手最多打5盘,其他地方最多打3盘)
  • 有些比赛部分是强制性的,除非比赛完全取消,而另一些部分是可选的(足球加时赛/点球赛、网球平局休息)。如果某个特定的匹配已经完成,但没有可选的部分,那么您就不必为它们创建记录
  • 比赛部分由若干子部分组成(同样在网球比赛中,成套比赛由比赛组成),因此您可能需要组织一个层次结构。您可以创建一个单独的子零件表,或将其全部放在一起;这取决于您,如果您对SQL足够了解,这两种方法都是可行的
  • 根据分数类型,同一比赛部分可能有多个分数记录。例如,在足球比赛中,这将是进球、任意球/角球、黄牌/红牌、点球等。在网球比赛中,额外的得分类型可能是a和双误,仅举几个例子
请记住,这份名单绝对不完整。可以说,我只是触及了表面

考虑到所有这些,生成的表可能如下所示:

create table dbo.MatchParts (
Id bigint identity(1,1) primary key,
MatchId int not null references dbo.Matches (Id),
-- Match part type lookup, big thing in itself
PartTypeId int not null references dbo.MatchPartTypes (Id),
-- Parent part reference for sub-part records, in case you decide to store them all in one table
ParentPartId bigint null references dbo.MatchParts (Id),
-- Position # of part within match or parent
SequenceNumber smallint not null,
-- Score type lookup
ScoreTypeId int not null references dbo.ScoreTypes (Id),
Score1 smallint null,
Score2 smallint null,
-- Natural key. Depends... on many other design decisions.
unique (MatchId, PartTypeId, ParentPartId, ScoreTypeId, SequenceNumber)
);

我强烈建议从主题领域的深入商业分析开始。它可能看起来很简单,但事实并非如此。

您能否提供一个示例,说明您的
匹配
实体的外观?听起来好像你把两个部分都放进了匹配本身,因此数据模型几乎不可扩展。@RogerWolf我现在已经添加了这个类。
create table dbo.MatchParts (
Id bigint identity(1,1) primary key,
MatchId int not null references dbo.Matches (Id),
-- Match part type lookup, big thing in itself
PartTypeId int not null references dbo.MatchPartTypes (Id),
-- Parent part reference for sub-part records, in case you decide to store them all in one table
ParentPartId bigint null references dbo.MatchParts (Id),
-- Position # of part within match or parent
SequenceNumber smallint not null,
-- Score type lookup
ScoreTypeId int not null references dbo.ScoreTypes (Id),
Score1 smallint null,
Score2 smallint null,
-- Natural key. Depends... on many other design decisions.
unique (MatchId, PartTypeId, ParentPartId, ScoreTypeId, SequenceNumber)
);