2 回答

TA貢獻1827條經(jīng)驗 獲得超8個贊
假設 DTO 為
public class Document
{
int Id { get; set; }
string DocumentName { get; set; }
bool IsNew { get; set; } // This field is not in the database
}
我可以使用這個事件處理程序:
private void Documents_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach(Document item in e.NewItems)
{
item.IsNew = true;
}
}
標記用戶添加到數(shù)據(jù)網(wǎng)格的任何新記錄。從數(shù)據(jù)庫加載原始記錄后,我鉤住了這個處理程序:
public void LoadDocuments()
{
var documents = myIdbConnection.GetAll<Document>();
Documents = new ObservableCollection<Document>(documents);
Documents.CollectionChanged += Documents_CollectionChanged;
}
接著:
public void Save()
{
myIdbConnection.Update(Documents.Where(x=>!x.IsNew));
myIdbConnection.Insert(Documents.Where(x=>x.IsNew));
}

TA貢獻1860條經(jīng)驗 獲得超8個贊
您可以省去事件監(jiān)聽。只需使用另一個值對新記錄進行默認初始化即可。
public class Document
{
static bool IsNewInitializer { get; set; } = false;
int Id { get; set; }
string DocumentName { get; set; }
bool IsNew { get; set; } = IsNewInitializer; // This field is not in the database
}
public void LoadDocuments()
{
Document.IsNewInitializer = false;
var documents = myIdbConnection.GetAll<Document>();
Documents = new ObservableCollection<Document>(documents);
Document.IsNewInitializer = true;
}
public void Save()
{
myIdbConnection.Update(Documents.Where(x => !x.IsNew));
myIdbConnection.Insert(Documents.Where(x => x.IsNew));
foreach (var d in Documents.Where(x => x.IsNew))
{
d.IsNew = false;
}
}
- 2 回答
- 0 關注
- 149 瀏覽
添加回答
舉報