我真的是一个LINQ新手。我有一个未知的问题:
public static int save(TEntity obj)
{
var table = dbo.GetTable<TEntity>();
var mapping = dbo.Mapping.GetTable(typeof(TEntity));
var pkfield = mapping.RowType.DataMembers.Where(d => d.IsPrimaryKey).Take(1).SingleOrDefault();
if (Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null)) == 0)
table.InsertOnSubmit(obj);
try
{
dbo.SubmitChanges();
}
catch (ChangeConflictException e)
{
dbo.SubmitChanges();
}
if (dbo.ChangeConflicts.Count == 0)
{
ClearCache(dbo);
return Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null));
}
else
{
dbo.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepCurrentValues);
return 0;
}
}
使用此方法时,只有1个字段已更新!!这是我的日志:
UPDATE [dbo].[tbl_album]
SET [dt_m_date] = @p1
WHERE [i_album_id] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [1256485605]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4918
即使我改变了几乎所有的字段,我的表已经有了主键。但还是有问题。
请帮忙!