我在Windows Phone(SQL Server CE)的LINQ to SQL中遇到一个问题。我正在开发一个个人理财应用程序,然后有Account类和Transaction类。每个Transaction类都有对其所属帐户的引用,因此Account类具有一对多关系的交易集合。然后,我有了存储库(AccountRepository和TransactionRepository),这些存储库公开了用于插入,删除,findbykey和返回每个此类的所有实例的方法。 ViewModel引用了其存储库。好的,一切工作都很好,但是,当我创建交易时,它不会出现在Account.Transactions集合中,直到我停止该软件并再次运行它。这是一些代码,首先是模型类:[Table]public class Transaction : INotifyPropertyChanged, INotifyPropertyChanging{ // {...} [Column] internal int _accountID; private EntityRef<Account> _account; [Association(Storage = "_account", ThisKey = "_accountID")] public Account Account { get {return _account.Entity;} set { NotifyPropertyChanging("Account"); _account.Entity = value; if (value != null) { _accountID = value.AccountID; } NotifyPropertyChanged("Account"); } } // {...}}[Table]public class Account : INotifyPropertyChanged, INotifyPropertyChanging{ // {...} private EntitySet<Transaction> _transactions; [Association(Storage = "_transactions", OtherKey = "_accountID")] public EntitySet<Transaction> Transactions { get { return this._transactions; } set { this._transactions.Assign(value); } } public Account() { _transaction = new EntitySet<Transaction>( new Action<Transaction>(this.attach_transaction), new Action<Transaction>(this.detach_transaction) ); } private void attach_transaction(Transaction transaction) { NotifyPropertyChanging("Transactions"); transaction.Account = this; } private void detach_transaction(Transaction transaction) { NotifyPropertyChanging("Transactions"); transaction.Account = null; } // {...}}然后,我有一些实现了GetAll()方法的存储库,该方法返回一个ObservableCollection。存储库引用了在ViewModel类内部创建的Context类,如下所示:public class AccountRepository{ private MyContext _context; public AccountRepository(ref MyContext context) { _context = context; } // {...} public ObservableCollection<Account> GetAll() { return new ObservableCollection(_context.Accounts.Where([some paramethers]).AsEnumerable()); } // {...}}我的ViewModel初始化构造函数中的存储库,然后使用一些逻辑代码来公开方法,以插入,删除等每种类型。public class MyViewModel : INotifyPropertyChanged, INotifyPropertyChanging{ private MyContext _context; private AccountRepository accountRepository; private TransactionRepository transactionRepository; public ObservableCollection<Account> AllAccounts; public ObservableCollection<Transaction> AllTransactions; public MyViewModel(string connectionString) { _context = new MyContext("Data Source=’isostore:/mydatabase.sdf’"); if (!db.DatabaseExists()) db.CreateDatabase(); accountRepository = new AccountRepository(ref _context); transactionRepository = new TransactionRepository(ref _context); // [Some other code] LoadCollections(); } // {...} public void LoadCollections() { AllAccounts = accountRepository.GetAll(); NotifyPropertyChanged("AllAccounts"); AllTransactions = transactionRepository.GetAll(); NotifyPropertyChanged("AllTransactions"); } public void InsertTransaction(Transaction transaction) { AllTransactions.Add(transaction); transactionRepository.Add(transaction); LoadCollections(); // Tried this to update the Accounts with newer values, but don't work... } // {...}}用户创建事务时,页面将在视图模型中调用InsertTransaction(事务事务)方法,该方法将事务对象传递到存储库。但是Account对象中的Transactions集合不会得到更新。然后,我尝试调用LoadCollections()方法以在上下文中强制执行新查询,并尝试以某种方式获取一个新的帐户对象,但是它仍然没有最近创建的事务。如果我停止我的应用程序并再次启动它,则这些帐户是最新的,并且具有我上次运行时在其交易记录集合中创建的所有交易记录。如何在运行时更新此Transactions集合?更新问题:我收到一些有关通知UI集合已更改的反馈。我认为交易和帐户之间的关联存在问题。创建交易后,该交易将不会出现在account.Transactions集合中,直到我处置上下文并再次创建它。可能是某些通知错误,但我不认为是,我做了一些代码来证明这一点,我现在不在我的电脑中,但我将尝试解释我的测试。我为证明它的代码是这样的:Account c1 = context.Accounts.Where(c => c.AccountID == 1).SingleOrDefault();Transaction t1 = new Transaction() { Account = c1, {...} };context.Transactions.InsertOnSubmit(t1);context.SaveChanges();c1 = context.Accounts.Where(c => c.AccountID == 1).SingleOrDefault();// The transaction IS NOT in the c1.Transactions collection right NOW.context.Dispose();context = new MyContext({...});c1 = context.Accounts.Where(c => c.AccountID == 1).SingleOrDefault();// The transaction IS in the c1.Transactions after the dispose!Account c2 = context.Where(c => c.AccountID == 2).SingleOrDefault();t1 = context.Transactions.Where(t => t.TransactionID == x).SingleOrDefault();t1.Account = c2;context.SubmitChanges();c1 = context.Accounts.Where(c => c.AccountID == 1).SingleOrDefault();// The transaction still in the c1 collection!!!c2 = context.Accounts.Where(c => c.AccountID == 2).SingleOrDefault();// It should be in the c2 collection, but isn't yet...context.Dispose();context = new MyContext({...});c1 = context.Accounts.Where(c => c.AccountID == 1).SingleOrDefault();// The transaction is not in the c1.Transaction anymore!c2 = context.Accounts.Where(c => c.AccountID == 2).SingleOrDefault();// The transaction IS in the c2.Transactions now! 最佳答案 如果可以发布一个更完整的代码示例来演示特定问题,那将是很好的。就让Parent-> Child one:many关系正常工作而言,这不是L2S不能直接实现的功能。而是在DataContext中实现。这是通过使子代将其自身添加到其父EntitySet实例而在Child.Parent属性设置器中完成的。只要您在运行时通过使用维护此绑定的setter生成parent child关系,此机制就起作用。通过代码示例,这是由Visual Studio中的O / R设计器生成的属性,用于为子对象上的一对多关系分配父实体类型:[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Account_Transaction", Storage="_Account", ThisKey="AccountId", OtherKey="AccountId", IsForeignKey=true)] public Account Account { get { return this._Account.Entity; } set { Account previousValue = this._Account.Entity; if (((previousValue != value) || (this._Account.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._Account.Entity = null; previousValue.Transactions.Remove(this); } this._Account.Entity = value; if ((value != null)) { value.Transactions.Add(this); this._AccountId = value.AccountId; } else { this._AccountId = default(long); } this.SendPropertyChanged("Account"); } } }请注意,Transaction.Add和Transaction.Remove件...这是父项上EntitySet 的管理-它不会在L2S层中自动发生。是的,这是很多胶水。抱歉。我的建议是,如果您有一个具有很多关系的复杂数据模型,则可以使用DBML对其建模并让VS为您提供数据上下文。要删除该DataContext对象上的某些构造函数,需要进行一些有针对性的更改,但是设计人员吐出的99%可以正常工作。-约翰·加拉多(John Gallardo)Windows Phone开发人员。关于c# - LINQ to SQL关系不会更新集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7555276/
10-10 07:38