【问题标题】:Grid binded to EntityFramework Poco class via BindingSource is not automatically refreshing网格通过绑定源绑定到实体框架 Poco 类不会自动刷新
【发布时间】:2013-07-30 10:28:04
【问题描述】:

这是一个测试项目来展示我的问题。 (VS2012, WinForms, EntityFramework 5, XtraGrid 12.5)

由 EF PowerTools - 逆向工程师 CodeFirst 工具创建的模型。

在 timer1_tick 事件中,我正在更改 mypoco.value 属性。我期待 grid.cell 会自动显示此更改,但不会。我也尝试过使用文本框,但还是一样。

如果我在 timer1_tick 中取消注释 BindingSource.ResetCurrentItem() 可以正常工作,但这不是我的问题。如果我强制网格(或文本框)刷新一切都很好。

我希望 ef 创建的代理对象通过接口、方法或继承通知 DbSet.Local (ObservableCollection) -> BindingList -> BindingSource -> Grid 等,或者我不知道......我在问这个通知系统为什么不工作?或者它正在工作,但我的期望是错误的? (

为什么这没有按预期工作,我在哪里失败了?另请阅读代码中的注释。

谢谢。

//FORM CODE    
public partial class Form1 : Form
{

    testContext context = new testContext();
    MyPOCO mypoco;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        mypoco = context.MyPOCOes.Create();
        // mypoco is created but not proxied currently. state = detached

        // After adding it context proxy created and change tacking will be available
        context.MyPOCOes.Add(mypoco);

        // mypoco is in the memory but not saved to database. This is why using Local 
        myPOCOBindingSource.DataSource = context.MyPOCOes.Local.ToBindingList();

        // Setup timer
        timer1.Interval = 15 * 1000;
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Change the property and then warn user about this event occured 
        // At this point mypoco is proxied
        mypoco.Value = 99;
        this.Text = "Timer Tick";

        //myPOCOBindingSource.ResetCurrentItem();

    }

}

// some code from Form1.Designer file
private System.Windows.Forms.BindingSource myPOCOBindingSource;

private void InitializeComponent()
{
   this.myPOCOBindingSource = new System.Windows.Forms.BindingSource();
   ....
   this.myPOCOGridControl.DataSource = this.myPOCOBindingSource;
}




//MYPOCO
public partial class MyPOCO
{
    public int ID { get; set; }
    public Nullable<int> Value { get; set; }
}


//MAPPING
public class MyPOCOMap : EntityTypeConfiguration<MyPOCO>
{
    public MyPOCOMap()
    {
        // Primary Key
        this.HasKey(t => t.ID);

        // Table & Column Mappings
        this.ToTable("MyPOCO");
        this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.Value).HasColumnName("Value");
    }
}


//CONTEXT
public partial class testContext : DbContext
{
    static testContext()
    {
        Database.SetInitializer<testContext>(null);
    }

    public testContext()
        : base("Name=testContext")
    {
    }

    public DbSet<MyPOCO> MyPOCOes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MyPOCOMap());
    }
}

【问题讨论】:

    标签: c# .net entity-framework poco xtragrid


    【解决方案1】:

    MyPoco 的代理代码在这里:Nothing related on bindings (of course) ...

    public sealed class MyPOCO_F874E881B0FD3EF02199CD96C63396B451E275C5116C5DFBE892C68733857FDE : MyPOCO, IEntityWithChangeTracker, IEntityWithRelationships
    {
        [NonSerialized]
        private IEntityChangeTracker _changeTracker;
        private static Func<object, object, bool> _compareByteArrays;
        [NonSerialized, IgnoreDataMember, XmlIgnore, ScriptIgnore]
        public object _entityWrapper;
        private System.Data.Objects.DataClasses.RelationshipManager _relationshipManager;
        private static Action<object> _resetFKSetterFlag;
    
        private void EntityMemberChanged(string text1)
        {
            if (this._changeTracker != null)
            {
                this._changeTracker.EntityMemberChanged(text1);
            }
        }
    
        private void EntityMemberChanging(string text1)
        {
            if (this._changeTracker != null)
            {
                this._changeTracker.EntityMemberChanging(text1);
            }
        }
    
        public void SetChangeTracker(IEntityChangeTracker tracker1)
        {
            this._changeTracker = tracker1;
        }
    
        public override int ID
        {
            get
            {
                return base.ID;
            }
            set
            {
                if (base.ID != value)
                {
                    try
                    {
                        this.EntityMemberChanging("ID");
                        base.ID = value;
                        this.EntityMemberChanged("ID");
                    }
                    finally
                    {
                        _resetFKSetterFlag(this);
                    }
                }
            }
        }
    
        public System.Data.Objects.DataClasses.RelationshipManager RelationshipManager
        {
            get
            {
                if (this._relationshipManager == null)
                {
                    this._relationshipManager = System.Data.Objects.DataClasses.RelationshipManager.Create(this);
                }
                return this._relationshipManager;
            }
        }
    
        public override int? Value
        {
            get
            {
                return base.Value;
            }
            set
            {
                try
                {
                    this.EntityMemberChanging("Value");
                    base.Value = value;
                    this.EntityMemberChanged("Value");
                }
                finally
                {
                    _resetFKSetterFlag(this);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2011-12-05
      相关资源
      最近更新 更多