Code first と生成された SQL データベースに問題がありました。
雄牛と牛に関するアプリケーションです
ここに私のクラスがあります:
public class Animal : BaseEntity
{
public virtual int? DeathId { get; set; }
public virtual Death Death { get; set; }
public virtual int? SellId { get; set; }
public virtual Sell Sell { get; set; }
}
public class Death : Event
{
public virtual int AnimalId { get; set; }
public virtual Animal Animal { get; set; }
}
public class Sell : Event
{
public virtual int AnimalId { get; set; }
public virtual Animal Animal { get; set; }
}
これにより、次の SQL スクリプトが生成されます。
create table [dbo].[Animal] (
[Id] [int] not null identity,
[DeathId] [int] null,
[SellId] [int] null,
primary key ([Id])
);
create table [dbo].[Death] (
[Id] [int] not null,
[AnimalId] [int] not null,
primary key ([Id])
);
create table [dbo].[Sell] (
[Id] [int] not null,
[AnimalId] [int] not null,
primary key ([Id])
);
すべて正常ですが、問題は次のクラスにあります。
public class Servicie : Event
{
public virtual int MaleId { get; set; }
public virtual Male Male { get; set; }
public virtual int FemaleId { get; set; }
public virtual Female Female { get; set; }
public virtual int? ChildbirthId { get; set; }
public virtual Childbirth Childbirth { get; set; }
}
public class Childbirth : Event
{
public virtual int ServicieId { get; set; }
public virtual Servicie Servicie { get; set; }
public virtual int FemaleId { get; set; }
public virtual Female Female { get; set; }
}
これらのクラスは、次の SQL スクリプトを生成します。
create table [dbo].[Service] (
[Id] [int] not null,
[MaleId] [int] not null,
[FemaleId] [int] not null,
[ChildbirthId] [int] null,
primary key ([Id])
);
create table [dbo].[Parto] (
[Id] [int] not null,
[Servicie_Id] [int] not null,
[ServicieId] [int] not null,
[FemaleId] [int] not null,
primary key ([Id])
);
ご覧のとおり、「ServicieId」と「Servicie_Id」が最後に自動追加されています。
すべてが 1 対 0..1 の関係で、違いがわかりません。誰か助けてもらえますか?