1

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 の関係で、違いがわかりません。誰か助けてもらえますか?

4

1 に答える 1

0

編集 - テストアプリ全体を追加:

このモデルを使用したとき(あなたの簡易版)。Required アノテーションを追加して、どちらがプリンシパルでどちらが従属エンドであるかを EF が認識できるようにする必要があり、混乱しました。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace CFExamples2
{
    public class Servicio 
    {
        public int Id { get; set; }

        public int? PartoId { get; set; }
        public virtual Parto Parto { get; set; }
    }

    public class Parto
    {
        public int Id { get; set; }
        public virtual int ServicioId { get; set; }
        [Required]
        public virtual Servicio Servicio { get; set; }
    }


    public class Model : DbContext
    {
        public DbSet<Servicio> Servicios { get; set; }
        public DbSet<Parto> Partos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

        }
    }
    class Program
    {
        static void Main(string[] args)
        {


        }
    }
}

それはこれを生み出しました:

ここに画像の説明を入力

于 2013-03-01T16:28:03.557 に答える