Entity framework EF必需为可选关系,不使用FK的正确列

Entity framework EF必需为可选关系,不使用FK的正确列,entity-framework,foreign-keys,ef-fluent-api,identity-column,Entity Framework,Foreign Keys,Ef Fluent Api,Identity Column,我已经读了很多问题,但仍然找不到解决问题的方法 我正试着画一些像这样的东西,我画得很快 第一次尝试: public class CarMap : EntityTypeConfiguration<Car> { public CarMap() { ToTable("Cars", Constants.Schema); Property(t => t.EngineId);

我已经读了很多问题,但仍然找不到解决问题的方法

我正试着画一些像这样的东西,我画得很快

第一次尝试:

public class CarMap : EntityTypeConfiguration<Car>
    {
        public CarMap()
        {
            ToTable("Cars", Constants.Schema);
            Property(t => t.EngineId);

            HasRequired(x => x.SPropertyPolicyInsurance).WithMany().HasForeignKey(x => x.SPropertyPolicyInsuranceId);
        }
    }



public class Car : MyBase
    {
        public int EngineId { get; set; }
        public virtual Engine Engine { get; set; }
    }


public class EngineMap : EntityTypeConfiguration<Engine>
    {
        public EngineMap()
        {
            ToTable("Engines", Constants.Schema);

            Property(t => t.MyField1).HasColumnType("nvarchar").HasMaxLength(128).IsRequired();
            Property(t => t.MyField2).HasColumnType("nvarchar").HasMaxLength(128).IsRequired();
            Property(t => t.MyField3).HasColumnType("bit").IsRequired();            
        }
    }


public class Engine : MyBase
    {
        public string MyField1 { get; set; }
        public string MyField2 { get; set; }
        public bool MyField3 { get; set; }

        public virtual Car Car { get; set; }
    }
公共类CarMap:EntityTypeConfiguration
{
公共CarMap()
{
ToTable(“Cars”,Constants.Schema);
属性(t=>t.EngineId);
HasRequired(x=>x.spropertopolicyinsurance)。WithMany().HasForeignKey(x=>x.spropertopolicyinsuranceId);
}
}
公车:MyBase
{
公共int引擎ID{get;set;}
公共虚拟引擎引擎{get;set;}
}
公共类EngineMap:EntityTypeConfiguration
{
公共引擎映射()
{
ToTable(“引擎”,常量.Schema);
属性(t=>t.MyField1).HasColumnType(“nvarchar”).HasMaxLength(128.IsRequired();
属性(t=>t.MyField2).HasColumnType(“nvarchar”).HasMaxLength(128.IsRequired();
属性(t=>t.MyField3).HasColumnType(“位”).IsRequired();
}
}
公共类引擎:MyBase
{
公共字符串MyField1{get;set;}
公共字符串MyField2{get;set;}
公共bool MyField3{get;set;}
公共虚拟汽车{get;set;}
}
使用此解决方案,它会在我的引擎表上创建一个我不想要的新列

第二次尝试:

public class CarMap : EntityTypeConfiguration<Car>
    {
        public CarMap()
        {
            ToTable("Cars", Constants.Schema);
            Property(t => t.EngineId);

            HasRequired(x => x.SPropertyPolicyInsurance).WithMany().HasForeignKey(x => x.SPropertyPolicyInsuranceId);
        }
    }



public class Car : MyBase
    {
        public int EngineId { get; set; }
        public virtual Engine Engine { get; set; }
    }


public class EngineMap : EntityTypeConfiguration<Engine>
    {
        public EngineMap()
        {
            ToTable("Engines", Constants.Schema);

            Property(t => t.MyField1).HasColumnType("nvarchar").HasMaxLength(128).IsRequired();
            Property(t => t.MyField2).HasColumnType("nvarchar").HasMaxLength(128).IsRequired();
            Property(t => t.MyField3).HasColumnType("bit").IsRequired();            
        }
    }


public class Engine : MyBase
    {
        public string MyField1 { get; set; }
        public string MyField2 { get; set; }
        public bool MyField3 { get; set; }

        public virtual Car Car { get; set; }
    }
将CarMap更改为以下代码:

public class CarMap : EntityTypeConfiguration<Car>
    {
        public CarMap()
        {
            ToTable("Cars", Constants.Schema);
            Property(t => t.EngineId);

            HasRequired(x => x.Engine).WithOptional(p => p.Car);
        }
    }
公共类CarMap:EntityTypeConfiguration
{
公共CarMap()
{
ToTable(“Cars”,Constants.Schema);
属性(t=>t.EngineId);
has必需(x=>x.Engine)。带可选(p=>p.Car);
}
}
问题是它没有使用列EngineId来存储我的FK键值


非常感谢您的帮助…

不要紧,它现在可以工作了,我清理了所有代码,删除了可能有冲突的额外关系,现在使用以下Fluent API关系,它可以正常工作,只使用FK(EngineId)的一列

产生的代码优先迁移如下所示:

CreateTable(
     "myschema.Engines",
       c => new
       {
          Id = c.Int(nullable: false),
          myfield = c.String(nullable: false, maxLength: 128),
          myfield2 = c.String(nullable: false, maxLength: 128),
          myfield3 = c.Boolean(nullable: false),
       })
  .PrimaryKey(t => t.Id)
  .ForeignKey("basechema.Engines", t => t.Id)
  .Index(t => t.Id);


CreateTable(
     "myschema.Cars",
        c => new
        {
            Id = c.Int(nullable: false),
            EngineId = c.Int(nullable: false),
        })
   .PrimaryKey(t => t.Id)
   .ForeignKey("baseschema.Cars", t => t.Id)
   .ForeignKey("myschema.Engines", t => t.EngineId)
   .Index(t => t.Id)
   .Index(t => t.EngineId);
我希望它能帮助其他人;)