在ReminderSetting中设置不映射。为什么? 我尝试获取列表ReminderSetting,但他没有映射嵌套对象设置。 求你帮帮我。
[Table("ReminderSetting")]
public class ReminderSetting
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int SettingId { get; set; }
public Setting Setting { get; set; }
public string Login { get; set; }
public bool Value { get; set; }
}
[Table("Settings")]
public class Setting
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Title { get; set; }
}
不工作
modelBuilder.Entity<Setting>();
modelBuilder.Entity<ReminderSetting>()
.HasRequired(x => x.Setting)
.WithMany()
.HasForeignKey(k => k.SettingId);
不工作
modelBuilder.Entity<Setting>();
modelBuilder.Entity<ReminderSetting>()
.HasRequired(x => x.Setting)
.WithRequiredPrincipal();
答案 0 :(得分:0)
我认为以下内容适用于创建ReminderSettings和Settings之间的一对一关系:
[Table("ReminderSetting")]
public class ReminderSetting
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Setting")]
public int SettingId { get; set; }
public string Login { get; set; }
public bool Value { get; set; }
public virtual Setting Setting { get; set; }
}
[Table("Settings")]
public class Setting
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Title { get; set; }
public virtual ReminderSetting ReminderSetting { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ReminderSetting>()
.HasKey(r => r.SettingId);
modelBuilder.Entity<ReminderSetting>()
.Property(r => r.SettingId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<ReminderSetting>()
.HasRequired(r => r.Setting)
.WithRequiredDependent(s => s.ReminderSetting);
base.OnModelCreating(modelBuilder);
}