0

すでに多くの質問を読んでいますが、私の問題の解決策はまだ見つかりませんでした。私はこのような何かを持ってしようとしていますFKに適切な列を使用しない任意の関係にEFが必要

は、私は非常に迅速に

enter image description here

まず試しdrawed:それは私のエンジンのテーブルに新しい列を作成し、このソリューションを使用して

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; } 
    } 

こと私はしたくない...

2回目:

次のコードにCarMapを変更する:問題は、それが私のFKキーの値を格納する列ENGINEIDを使用していないということです

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

      HasRequired(x => x.Engine).WithOptional(p => p.Car); 
     } 
    } 

...

はのためのありがとうございましたとにかく助けてください...

答えて

0

私はすべてのコードを整理し、矛盾していた可能性のある余分な関係を削除しました。次に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); 

は、私はそれがそこに誰かに役立ちます願っています;)

関連する問題