【发布时间】:2011-03-10 09:46:48
【问题描述】:
我正在使用 CodeFirst CTP5。
正如您在我的代码中看到的,我有一个用户有很多问题。我希望能够删除用户但保留问题。另外,我还想保留问题的“UserId”属性
public class User
{
public User()
{
Questions = new List<Question>();
}
public virtual string UserId { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
public class Question
{
public virtual string QuestionId { get; set; }
public virtual string Title { get; set; }
public virtual string Text { get; set; }
public User User { get; set; }
public string UserId { get; set; }
}
public class DB : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Question> Questions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasMany(u => u.Questions)
.WithRequired(q => q.User)
.HasForeignKey(q => q.UserId)
.WillCascadeOnDelete(false);
}
}
问题是像这样配置 ModelCreating 会给我这个错误:
System.Data.Edm.EdmAssociationType: : 多重性在角色中无效 'Expert_Answers_Source' 在 关系'Expert_Answers'。因为 Dependent 中的所有属性 角色可以为空,多重性 主要角色必须是“0..1”
我做错了什么?我怎样才能得到它?
【问题讨论】:
标签: c# entity-framework-4 ef-code-first code-first