2011-10-20 7 views
1

私は古いアプリケーションをMVC 3に移植し、EFのコードをまず使用して既存のデータベーススキーマを複製したいと考えています。現在のコードベースは、ハードコードされたSQLコマンドで盛り込まれているので、私が考えているモデルは、現在のシステムが期待しているものと "互換性"がなければなりません。私はEFのDatabase Firstを使用することができると知っていますが、既存のスキーマはとてもシンプルなので、まずコードを使用しない理由はありません。古いハードコーディングされたデータベース相互作用。EFコード1対1マッピングと双方向ナビゲーションプロパティ

public class Owner 
{ 
    [Key] 
    public Guid Owner_Id { get; set; } 

    // Properties 
    public string ContactName { get; set; } 
    public string PhoneNumber { get; set; } 
    public string Email { get; set; } 

    // Navigational properties 
    public virtual ICollection<Plant> Plants { get; set; } 
} 

public class Plant 
{ 
    [Key] 
    public Guid Plant_Id { get; set; } 

    // Properties 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string Zip { get; set; } 

    // Foreign Keys 
    public Guid Owner_Id { get; set; } 

    // Navigational properties 
    public virtual Owner Owner { get; set; } 
    public virtual License License { get; set; } 
} 

public class License 
{ 
    [Key] public Guid License_Id { get; set; } 

    // Properties 
    public int Total { get; set; } 
    public int Users { get; set; } 
    public string Key { get; set; } 

    // Foreign Keys 
    public Guid Plant_Id { get; set; } 

    // Navigational properties 
    public virtual Plant Plant { get; set; } 
} 

それはコンテキストを作成しようとする上で私は、このエラーを与えている:

を「タイプ間の関連の主要な終了を決定することができません "私はPOCOSが見えるように必要なもの

この関連の主要な終点は、流暢なAPIまたはデータアノテーションのどちらかの関係を使用して明示的に構成されなければなりません。

Plantには、License_Idへのnull可能なFK参照が必要であり、そのライセンスにPlant_IdのFKがあるべきではないことが分かりました。しかし、これらは私が扱っているカードです。私はEFで可能なことをしようとしていますか?それは、これは他の外部キーに同じ外部キーであることを知っているように、ライセンス

// Foreign Keys 
public Guid Plant_Id { get; set; } 

// Navigational properties 
[ForeignKey("Plant_Id")] 
public virtual Plant Plant { get; set; } 

に追加

答えて

1
public class Plant 
{ 
    public Guid PlantID { get; set; } 
    public virtual License License { get; set; } 
} 

public class License 
{ 
    // No need to have a PlantID foreign key since the foreign key. 
    public Guid LicenseID { get; set; } 
    public virtual Plant Plant { get; set; } 
} 

(あなたのコンテキストクラスで):

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Plant>().HasOptional(p => p.License).WithRequired(l => l.Plant); 

唯一のダウングレードは、あなたが実際にゼロまたは1つ1つを定義していることです。 (これはコードで扱うことができますが、まだ...)1対1の関係の詳細については、Entity Framework Code First One to One Relationship

1

試してみます。

流暢-APIで
protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Plant>() 
      .HasOptional(e => e.License) 
      .WithOptionalPrincipal(e => e.Plant); 

    } 
+0

を取得しました。「System.Data.Edm.EdmAssociationEnd :: Multiplicity is関係 'License_Plant'のRole 'License_Plant_Source'では有効ではありません。依存ロールのプロパティはキープロパティではないため、依存ロールの多重度の上限は * mustでなければなりません。最終的にはちょっとばかばかしいことが何であるかはわからない... – Stevoman

+0

それは逆だった。 – Birey

+0

まだ、元のエラーを返すことに戻りません。 :( – Stevoman

関連する問題