C# EF添加已定义的FK

C# EF添加已定义的FK,c#,sql,entity-framework,ef-code-first,code-first,C#,Sql,Entity Framework,Ef Code First,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 {

我首先遇到了代码和生成的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])
);
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])
);
所有这些都正常,但问题在于这些类:

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].[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])
);
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])
);
如您所见,有“Serviceid”和“Servicie_Id”,最后一个是自动添加的


所有都是1对0..1关系,我看不出有任何区别,有人能帮我吗?

编辑-添加了整个测试应用程序:

当我使用这个模型时(你的简化版)。我必须添加一个必需的注释,以便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)
        {


        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用系统数据;
使用System.Data.Entity;
使用System.Linq;
使用系统文本;
名称空间CFExamples2
{
公共类服务
{
公共int Id{get;set;}
公共int?PartoId{get;set;}
公共虚拟部分{get;set;}
}
公共类零件
{
公共int Id{get;set;}
公共虚拟int服务ID{get;set;}
[必需]
公共虚拟服务服务{get;set;}
}
公共类模型:DbContext
{
公共数据库集服务{get;set;}
公共DbSet Partos{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
}
}
班级计划
{
静态void Main(字符串[]参数)
{
}
}
}
它产生了:


我试过了,结果发现:多重性在关系“Servicio\u Parto\u”中的角色“Servicio\u Parto\u Target”中无效。因为依赖角色属性不是关键属性,所以依赖角色的多重性上限必须是*角色必须是*在internet上搜索,我发现了这一点。看起来英孚并不完全支持与外国钥匙的一对一关系哦,我不知道这是1:1-是的,你不能有一个FK,为什么你不让它分享PK(英孚可以做到的唯一真实方式)我会试试,但百万美元的问题是为什么它与“动物”、“穆尔特”和“文塔”一起工作,而不是与“服务”和“帕托”一起工作?如果一切都是1-0..1关系我想Visual Studio想要让我发疯!我不能让它工作,你能和我分享这个示例项目吗?请简化和翻译你的代码示例。我真的说不出来,但我可以很好地猜测,其中大部分与你的问题无关。(就像您想要的实体的所有代码一样。)已翻译!如果你现在了解更多,请告诉我