Baza danych nie jest aktualizowana metodą Attach
Próbuję nauczyć się LINQ do SQL. Pomyślnie zaimplementowałem metodę wstawiania, a dane są wstawiane do bazy danych. Gdy próbuję zaktualizować istniejące dane, nie pojawia się ono w bazie danych, nawet jeśli nie ma wyjątku. Czy możesz rzucić trochę światła na to, co mogło pójść źle?
Uwaga: Numer konta to klucz podstawowy
EDYTOWAĆ
Kolejne wiersze kodu sprawiają, że działa. Ale możesz wyjaśnićdlaczego potrzebujemy odświeżenia?
accountRepository.UpdateChangesByAttach(acc1);
accountRepository.RefreshEntity(acc1);
accountRepository.SubmitChanges();
Uwaga:
DataContext.Refresh method refreshes object state by using data in the database.
Odświeżenie zostało wykonane za pomocą opcji KeepCurrentValues. Rozumiem, że zachowa wartości, które zaktualizowałem w obiekcie encji. Dostarczam (tylko) wymagane informacje, które należy zaktualizować w bazie danych. Czy wymagane jest odświeżenieuzyskać pozostałe wartości kolumn?
Klient
using (var context = new RepositoryLayer.LibraryManagementClassesDataContext(connectionstring))
{
context.Log = Console.Out;
RepositoryLayer.Repository<RepositoryLayer.Account> selectedRepository = new RepositoryLayer.Repository<RepositoryLayer.Account>();
//selectedRepository.Context = context;
AccountBusiness accountBl = new AccountBusiness(selectedRepository);
//Transaction 1
//List<RepositoryLayer.Account> accountList = accountBl.GetAllAccounts();
//Transaction 2
//accountBl.InsertAccounts();
//Transaction3
accountBl.UpdateAccounts();
}
Warstwa biznesowa
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();
}
Magazyn
public class Repository<T> : IRepository<T> where T : class
{
public System.Data.Linq.DataContext Context
{
get;
set;
}
public virtual System.Data.Linq.ITable GetTable()
{
return Context.GetTable<T>();
}
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);
}
}
CZYTANIE
LINQ to SQL: aktualizacja bez odświeżania, gdy „UpdateCheck = Never”
Linq To SQL Attach / Refresh Entity Object
Co robi tabela LINQ-to-SQL <T>. Dołącz?
Jak mogę odrzucić wszystkie zmiany w Linq do DataContext SQL?