0
有沒有人可以告訴我下面這段代碼有什麼問題,因爲我在第一次使用EF 4.3代碼時遇到了一對一的關係問題。EF 4.3代碼第一對一的關係
// 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
一直在嘗試各種各樣的東西,添加foreignkey屬性,(un)評論一些id的樣本。 結果總是:數據庫中的ID不正確,或者它不會創建數據庫。
在此先感謝。
確實「一直在努力」包括HTTP我已經通過流暢API在配置模型,覆蓋模型構建器像這樣的DbContext類中做到了: //stackoverflow.com/questions/5339951/one-to-one-relationship-on-primary-key-with-entity-framework-code-first? – 2012-02-15 21:35:28