EF 4.3コードで最初に動作する1対1の関係を取得する際に問題が発生するため、このコードで何が間違っているか教えてください。EF 4.3コード第1の1対1の関係
// Problem with EF 4.3 code first, one-to-one relation
// context
// ------------
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<SecondModel>().HasRequired(r => r.FirstModel).WithOptional(r => r.SecondModel).WillCascadeOnDelete(false);
}
// models
// --------
public abstract class MyBaseEntity : // some interfaces
{
// ...
[EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
public virtual int Id
{
get { return GetPropertyValue<int>("Id"); }
set { SetPropertyValue<int>("Id", value); }
}
// ...
}
public class FirstModel : MyBaseEntity
{
// ...
public int SecondModelID { get; set; }
public virtual SecondModel SecondModel { get; set; }
// ...
}
public class SecondModel : MyBaseEntity
{
// ...
public int FirstModelID
{
get { return GetPropertyValue<int>("FirstModelID"); }
set { SetPropertyValue<int>("FirstModelID", value); }
}
public virtual FirstModel FirstModel { get; set; }
// ...
}
// this code above doesn't seem to work :s
// when removing FirstModelID and removing SecondModelID i'm unable to create the the database
はサンプル以下、いくつかのIDのコメントのForeignKey属性、(UN)を追加し、あらゆる種類のものをしようとしています。 結果は常にでした:データベース内のIDが正しくないか、データベースが作成されません。
ありがとうございます。
ん:
とクラス:私はDbContextクラス内このようModelBuilderのを上書きし、流暢なAPIでモデルを設定することにより、それをやりました: //stackoverflow.com/questions/5339951/one-to-one-relationship-on-primary-key-withentent-framework-code-first? –