我有一个Tab对象。这个Tab对象有很多问题。这是我的课程:
public abstract class Tab
{
/// <summary> init default values </summary>
public Tab()
{
// Do some init stuff
}
public int ID { get;set; }
//...
/// <summary> Question for this Tap. Like Quiz-App or Sweepstake </summary>
public virtual ICollection<Tabs.Question> Questions { get; set; }
//...
}
public class Question
{
public int Id { get; set; }
public int OrderNumber { get; set; }
public string Text { get; set; }
public virtual Tab Tab { get; set; }
}
这是两个类的配置。
class TabConfig : EntityTypeConfiguration<Tab>
{
public TabConfig()
{
this.Map(m => m.Requires("AppType").HasValue<byte>((byte)ApplicationType.Application)).ToTable("TmTabs");
this.HasKey(c => c.ID);
this.Property(p => p.ID).HasColumnName("ID").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
public class QuestionConfig : EntityTypeConfiguration<Question>
{
public QuestionConfig()
{
ToTable("TmQuestions");
HasKey(q => q.Id);
Property(q => q.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(q => q.OrderNumber).HasColumnName("OrderNumber").IsRequired();
Property(q => q.Text).HasColumnName("Text").IsRequired();
HasRequired(q => q.Tab).WithMany(t => t.Questions).Map(q => q.MapKey("TmTabs_Id")).WillCascadeOnDelete(false);
}
}
并且配置将添加到这样的构建事件中。
public class ModelContext : DbContext
{
public Guid ContractId { get; set; }
#region construction
public ModelContext()
: base(Tools.DatabaseTools.GetDefaultConnectionString())
{
ContractId = Guid.Empty;
}
public ModelContext(Guid contractGuid)
: base(Tools.DatabaseTools.GetConnectionString(contractGuid))
{
ContractId = contractGuid;
}
public ModelContext(string connectionString)
: base(connectionString)
{
ContractId = Guid.Empty;
}
public ModelContext(string connectionString, Guid contractGuid)
: base(connectionString)
{
ContractId = contractGuid;
}
public DbSet<Tab> Tabs { get; set; }
public DbSet<Question> Questions { get; set; }
protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Conventions.Remove<PluralizingTableNameConvention>();
// tab-table
builder.Configurations.Add(new TabConfig());
// Qustions
builder.Configurations.Add(new ModelConfig.QuestionConfig());
base.OnModelCreating(builder);
}
}
在我的编辑表单上,我将整个Tab对象绑定到绑定Source,然后编辑Tab对象的一些属性,然后添加Questions。
如果我第一次保存Tab对象一切正常。但如果我编辑标签对象,我收到了这个错误:
Multiplicity constraint violated. The role 'Question_Tab_Target' of the relationship 'Tabmaker.Common.Question_Tab' has multiplicity 1 or 0..1.
Tab业务层的代码是:
public bool SaveTab<T>(ModelContext context, T tab) where T:Tab
{
try
{
// add if detached
if (tab.ID == 0)
{
// new tab code
}
// attach/set to be modified if id!=0. if tab is not detached, the property
// changes were already tracked and nothing need to be done here
var state = context.Entry(tab).State;
if (state == EntityState.Detached || state == EntityState.Deleted)
{
context.Entry(tab).State = EntityState.Modified;
}
context.SaveChanges();
return true;
}
catch (Exception ex)
{
// Catch
return false
}
}
如果我要求制表符实体的状态,则会发生错误:
var state = context.Entry(tab).State;
我希望我尽可能好地解释一切。