2016-07-29 9 views
2

既存の実動データベースを使用してMVCアプリケーションを作成していますが、データ型の不一致により、親クラスの仮想プロパティを正しく設定できません。次のように私のコードは次のとおりです。既存のデータベースをEFにマッピングするデータ型の不一致

public class bill { 

public int billId { get; set; } 

......... 

} 


public class meter { 

public int meterId { get; set; } 

public int billId { get; set; } 

..... 

} 


public class payment { 

public int paymentId { get; set; } 

public long billId { get; set; } 

...... 
} 

私は法案クラスに次を追加しました:

public virtual ICollection<meter> Meters { get; set; } 
public virtual ICollection<payment> Payments { get; set; } 

私は、関連メートルを取得することができていますが、支払期日におけるbillidの長いデータ型に私はエラーを取得します。元のデータベースに影響を与えるコードの最初のモデルに変更を加えなければ、これを正しくマッピングする方法はありますか?

私はエラーを取得しています:

Invalid column name 'bill_billId'.
Invalid column name 'bill_billId'.
Invalid column name 'bill_billId'.

次のように表示しよう:

現在使用中のデータベースが誤ってしばらく子クラス用の長いデータ型を使用するように設定されて

@foreach (var m in Model.Meters.Where(x => x.archived != true && x.meterUnitsUsed > 0)) 
{ 
     <tr> 
      <td>@Html.DisplayFor(x => m.meterSerialNumber)</td> 
      <td>@Html.DisplayFor(x => m.meterOpeningRead)</td> 
      <td>@Html.DisplayFor(x => m.meterClosingRead)</td> 
      <td>@Html.DisplayFor(x => m.meterUnitsUsed)</td> 
      <td>@Html.DisplayFor(x => m.archived)</td> 
      <td>@Html.DisplayFor(x => m.meterUnitType)</td> 
      <td>@Html.DisplayFor(x => m.meterUnitPrice)</td> 
      <td>@Html.DisplayFor(x => m.meterAmount)</td> 

      @foreach (var i in Model.LiftsRefunds.Where(i => i.archived != true)) 
      { 
       <td>@Html.DisplayFor(x => i.liftRefundDate)</td> 
       <td>@Html.DisplayFor(x => i.liftRefundAmount)</td> 
       <td>@Html.DisplayFor(x => i.liftType)</td> 
      } 
     </tr> 
} 
親クラスの主キーはintです。これを無視し、支払いを仮想プロパティとして参照できるようにintをlongにマップするEFを設定する方法はありますか?エラーのために

+0

さて、 'meter'クラスに' billId'をintとして持っています。それは長くてはいけませんか? – ESG

+0

さらに、エラーが発生していると言う場合は、常に**正確なエラーメッセージを提供する必要があります。テーブル定義も役立ちます。 – ESG

+0

メータークラスは正常に動作していますが、ペイメントテーブルにはbillidが長く設定されていますが、billidとmeteridで両方ともintに設定されています。私のコンテキストを変更してもデータベースは変更されません。 – Jay

答えて

1

を私はあなたが2つの異なる問題を抱えていると思います。

===すべての
まず、payment.billIdは、我々は、彼らがSQL Server上の2つの異なる列(テーブルごとに1つ)があるとしbill.billIdへの参照です。
1.マイグレーションを無視すると(実際にはマイグレーションを無視しても問題ありません)、SQL Serverのデータ型がSQL Serverのint型とbigint型のデータ型と異なる場合、プログラムは動作し、int型とint型あなたのクラスに。
2. BINT SQL Serverはbigint列からint列へのリレーションシップを作成できません。したがって、DBではbigintとintを2つの列に持つことはできません。また、関係もありませんEFはとにかく動作しますが、それは良い方法ではありません)。

SQL Server上で何をしているのかを確認し、クラスのデータ型を変更してください(つまり、すべてintを設定する)、移行を無効にすることをお勧めします。

===
ここで、存在しないフィールドについてのエラーはなぜですか?
クラスにマッピング設定の問題があります。 payment.billIdプロパティをpublic virtual ICollection<payment> Payments { get; set; }に接続する必要があります。この場合には、支払いのマッピングファイルで、私は

HasRequired(t => t.bill) 
    .WithMany(t => t.Payments) 
    .Map(d => d.MapKey("billId")); 

を書くよう は、私はあなたが属性と同じことを行うことができます流暢なマッピングとクラスごとに1つのマッピングファイルを使用します。

0

The 'billId' property on 'payment' could not be set to a 'System.Int64' value. You must set this property to a non-null value of type 'System.Int32'

が指示 here従います

In your EDMX, if you go under your Y table and click on X column, right-click, click on Properties, scroll down to Nullable and change from False to True.

If you get a "mapping fragment" error, you'll have to delete the table from the EDMX and re-add it, because in the Model Browser it stores the table properties and the only way to refresh that (that I know of) is to delete the table from the Model Browser under .Store then retrieving it using Update Model from Database.. command.

詳細については、MSDNのドキュメントを参照してください。How to: Update an .edmx File when the Database Changes (Entity Data Model Tools)

関連する問題