C# 通过BindingSource绑定到EntityFramework Poco类的网格不会自动刷新
这是一个测试项目来展示我的问题。(VS2012,WinForms,EntityFramework 5,XtraGrid 12.5) 由EF PowerTools创建的模型-反向工程CodeFirst工具 在timer1\u tick事件中,我正在更改mypoco.value属性。我希望grid.cell会自动显示这些更改,但不会。我也试过使用textbox,但都是一样的 如果我在timer1中取消注释BindingSource.ResetCurrentItem(),则勾选工作正常,但这不是我的问题。如果我强制网格(或文本框)刷新,一切都很好 我希望ef创建的代理对象通过接口、方法或继承或我不知道的方式通知DbSet.Local(ObservableCollection)->BindingList->BindingSource->Grid等。。。我在问这个通知系统为什么不工作?或者它正在工作,但我的期望是错误的?( 为什么这不按预期工作,我失败了?请阅读代码中的注释 多谢各位C# 通过BindingSource绑定到EntityFramework Poco类的网格不会自动刷新,c#,.net,entity-framework,poco,xtragrid,C#,.net,Entity Framework,Poco,Xtragrid,这是一个测试项目来展示我的问题。(VS2012,WinForms,EntityFramework 5,XtraGrid 12.5) 由EF PowerTools创建的模型-反向工程CodeFirst工具 在timer1\u tick事件中,我正在更改mypoco.value属性。我希望grid.cell会自动显示这些更改,但不会。我也试过使用textbox,但都是一样的 如果我在timer1中取消注释BindingSource.ResetCurrentItem(),则勾选工作正常,但这不是我的问
//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());
}
}
//表单代码
公共部分类Form1:Form
{
testContext=新的testContext();
MyPOCO-MyPOCO;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
mypoco=context.MyPOCOes.Create();
//mypoco已创建,但当前未代理。状态=已分离
//添加后,将创建上下文代理并更改跟踪
context.MyPOCOes.Add(mypoco);
//mypoco在内存中,但未保存到数据库。这就是为什么使用本地
myPOCOBindingSource.DataSource=context.MyPOCOes.Local.ToBindingList();
//设置计时器
定时器1.间隔=15*1000;
timer1.Start();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
//更改属性,然后警告用户发生此事件
//此时,mypoco已被代理
mypoco.Value=99;
this.Text=“Timer Tick”;
//myPOCOBindingSource.ResetCurrentItem();
}
}
//来自Form1.Designer文件的一些代码
private System.Windows.Forms.BindingSource myPOCOBindingSource;
私有void InitializeComponent()
{
this.myPOCOBindingSource=new System.Windows.Forms.BindingSource();
....
this.myPOCOGridControl.DataSource=this.myPOCOBindingSource;
}
//MYPOCO
公共部分类MyPOCO
{
公共int ID{get;set;}
公共可空值{get;set;}
}
//映射
公共类MyPOCOMap:EntityTypeConfiguration
{
公共MyPOCOMap()
{
//主键
this.HasKey(t=>t.ID);
//表和列映射
本.ToTable(“MyPOCO”);
this.Property(t=>t.ID).HasColumnName(“ID”);
this.Property(t=>t.Value).HasColumnName(“Value”);
}
}
//上下文
公共部分类testContext:DbContext
{
静态testContext()
{
Database.SetInitializer(null);
}
公共testContext()
:base(“Name=testContext”)
{
}
公共DbSet MyPOCOes{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
添加(新的MyPOCOMap());
}
}
这里是MyPoco的代理代码:与绑定无关(当然)
公共密封类MyPOCO_F874E881B0FD3EF02199CD96C63396B451E275C5116C5DFBE892C68733857FDE:MyPOCO,带ChangeTracker的客户端,带关系的客户端
{
[非串行化]
私有的IEntityChangeTracker\u changeTracker;
私有静态函数比较字节数组;
[非序列化,IgnoreDataMember,XmlIgnore,ScriptIgnore]
公共对象_entityWrapper;
私有系统.Data.Objects.DataClasses.RelationshipManager\u RelationshipManager;
私人静态动作_resetFKSetterFlag;
私有void EntityMemberChanged(字符串text1)
{
if(this.\u changeTracker!=null)
{
此._changeTracker.EntityMemberChanged(text1);
}
}
私有void EntityMemberChanging(字符串text1)
{
if(this.\u changeTracker!=null)
{
此._changeTracker.EntityMemberChanging(text1);
}
}
公共无效SetChangeTracker(IEntityChangeTracker tracker1)
{
这是._changeTracker=tracker1;
}
公共覆盖int-ID
{
得到
{
返回base.ID;
}
设置
{
if(base.ID!=值)
{
尝试
{
本文件为:实体成员变更(“ID”);
base.ID=值;
本公司全体成员已变更(“ID”);
}
最后
{
_resetFKSetterFlag(此);
}
}
}
}
public System.Data.Objects.DataClasses.RelationshipManager RelationshipManager
{
得到
{
if(this.\u relationshipManager==null)
{
this.\u relationshipManager=System.Data.Objects.DataClasses.relationshipManager.Create(this);
}
将此返回。\u relationshipManager;
}
}
公共覆盖int?值
{
得到
{
返回基本值;
}
设置
{
尝试
{
本协议项下的所有成员变更(“价值”);
基本价值=价值;
本公司全体成员已变更(“价值”);
}
最后
{
_resetFKSetterFlag(此);
}
}
}
}
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);
}
}
}
}