3

我有一个 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);

    }
}

在我的 Edit-Form 上,我将整个 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;

我希望我尽可能地解释一切。

4

0 回答 0