Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase does not get updated with Attach method
    primarykey
    data
    text
    <p>I am trying to learn LINQ to SQL. I have successfully implemented insert method and data is getting inserted into database. When I try to update the existing data, it does not get reflected in the database even though there is no exception. Can you please put some light on what could have gone wrong?</p> <p>Note: AccountNumber is primary key</p> <p><strong>EDIT</strong></p> <p>Following lines of code makes it working. But, can you explain <strong>why we need a refresh</strong>?</p> <pre><code> accountRepository.UpdateChangesByAttach(acc1); accountRepository.RefreshEntity(acc1); accountRepository.SubmitChanges(); </code></pre> <p>Note:</p> <pre><code>DataContext.Refresh method refreshes object state by using data in the database. </code></pre> <p>The refresh was done with KeepCurrentValues option. What I understand is it will retain the values which I updated in the entity object. I am supplying (only) the required information which need to be updated in the database. Is this refresh required <strong>to get the remaining column values?</strong></p> <p>Client</p> <pre><code>using (var context = new RepositoryLayer.LibraryManagementClassesDataContext(connectionstring)) { context.Log = Console.Out; RepositoryLayer.Repository&lt;RepositoryLayer.Account&gt; selectedRepository = new RepositoryLayer.Repository&lt;RepositoryLayer.Account&gt;(); //selectedRepository.Context = context; AccountBusiness accountBl = new AccountBusiness(selectedRepository); //Transaction 1 //List&lt;RepositoryLayer.Account&gt; accountList = accountBl.GetAllAccounts(); //Transaction 2 //accountBl.InsertAccounts(); //Transaction3 accountBl.UpdateAccounts(); } </code></pre> <p>Business Layer</p> <pre><code> public void InsertAccounts() { RepositoryLayer.Account acc1 = new RepositoryLayer.Account(); acc1.AccountNumber = 4; acc1.AccountType = "Contract"; acc1.Duration = 6; accountRepository.InsertOnSubmit(acc1); accountRepository.SubmitChanges(); } public void UpdateAccounts() { RepositoryLayer.Account acc1 = new RepositoryLayer.Account(); acc1.AccountNumber = 4; acc1.AccountType = "TEST"; acc1.Duration = 10; accountRepository.UpdateChangesByAttach(acc1); accountRepository.SubmitChanges(); } </code></pre> <p>Repository</p> <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class { public System.Data.Linq.DataContext Context { get; set; } public virtual System.Data.Linq.ITable GetTable() { return Context.GetTable&lt;T&gt;(); } public virtual void InsertOnSubmit(T entity) { GetTable().InsertOnSubmit(entity); } public virtual void UpdateChangesByAttach(T entity) { GetTable().Attach(entity); } public virtual void SubmitChanges() { Context.SubmitChanges(); } public virtual void RefreshEntity(T entity) { Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity); } } </code></pre> <p>READING</p> <ol> <li><p><a href="https://stackoverflow.com/questions/11189688/linq-to-sql-updating-without-refresh-when-updatecheck-never">LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”</a></p></li> <li><p><a href="https://stackoverflow.com/questions/898267/linq-to-sql-attach-refresh-entity-object?rq=1">Linq To SQL Attach/Refresh Entity Object</a></p></li> <li><p><a href="https://stackoverflow.com/questions/5244096/what-does-linq-to-sql-tablet-attach-do">What does LINQ-to-SQL Table&lt;T&gt;.Attach do?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/3472408/why-should-i-use-getoriginalentitystate-in-my-linq-to-sql-repository-save-meth">Why should I use GetOriginalEntityState() in my LINQ To SQL repository save method?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/259219/how-can-i-reject-all-changes-in-a-linq-to-sqls-datacontext">How can I reject all changes in a Linq to SQL&#39;s DataContext?</a></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload