2015年8月29日 星期六

EF 6 (Single and Bulk Update)

EF Extesnion loresoft

Bulk Update  (Directly delete and auto commit)
db.People.Where(it => it.DeptId =="101").Update(it => new People { Salary = it.Salary * 1.1 });

Bulk Delete (Directly delete and auto commit)
ex:
db.People.Where(it => it.DepId =="101").Delete();  // where db the the DbContext

EF update without select (Single Update)

var newPeople = new People { PeopleId = "0001" };  // where PeopleId is the key 
db.People.Attach(newPeople);
newPeople.{attribute1} = {new value1};  // assume there are 8 attributes in People object
newPeople.{attribute4} = {new value4};
newPeople.{attribute6} = {new value6};
db.SaveChanges();

after db.SaveChages(); sql will be generated like this update people  set attribute1 = newValue1, attribute4 = newValue4, attribute6 = newValue6 where PeopleId = '0001';  
system only update the changed items 

in another case (ref to this article https://msdn.microsoft.com/en-us/data/jj592676.aspx)
var newPeople = new People { PeopleId = "0001" };   // where PeopleId is the key 
db.Entry(newPoeple).State = EntityState.Modified;
newPeople.{attribute1} = {new value1};  // assume there are 8 attributes in People object
newPeople.{attribute4} = {new value4};
newPeople.{attribute6} = {new value6};
db.SaveChanges();

When you change the state to Modified all the properties of the entity will be marked as modified and all the property values will be sent to the database when SaveChanges is called. That means all attibutes in this entity will be populated to the database and the null value will be the default if you do not specify in above codes.

EF delete without select (Single Delete)
var newPeople = new People { PeopleId = "0001" };  // where PeopleId is the key 
db.Entry(newPoeple).State = EntityState.Deleted;
db.SaveChanges();
or

db.Entry(new People { PeopleId = "0001"} ).State = EntityState.Deleted;
db.SaveChanges();

2015年8月27日 星期四

Table 更新需要對應的跟新的 VIEW (sp_refreshview)

After creating a view, the meta data of the view will be stored and will not change even the table that relies is changed.

There are many ways to solve the problem. The first one is drop the view and create the view. Power Designer is good at that. This tool will generate the complete script to do that. However there is anther stored procedure to support this. it is sp_refreshview.

command example:
sp_refreshview v_name


In the mean time, we could run the following script to retrieve all the views that should be rerun sp_refreshview once the table is changed:


SELECT DISTINCT 'EXEC sp_refreshview ''' + name + ''''
FROM sys.objects so INNER JOIN sys.sql_dependencies sd
ON so.object_id = sd.object_id
WHERE type = 'V'
AND sd.referenced_major_id = object_id('TABLE_WHO_IS_CHANGED');

2015年8月18日 星期二

EF 6 + .NET 4 遇到資料載入錯誤的畫面

EF 6.1.3 + .NET Framework 4
若系統只有 .net 4 系統要執行 Telerik RadGrid 載入資料時, 系統將會出現類似下面的錯誤
Duranet2Model.csdl(2,9) : warning 0005: Could not find schema information for the attribute 'Namespace'.
Duranet2Model.csdl(2,35) : warning 0005: Could not find schema information for the attribute 'Alias'.
Duranet2Model.csdl(2,48) : error 0005: The 'http://schemas.microsoft.com/ado/2009/02/edm/annotation:UseStrongSpatialTypes' attribute is not declared.
Duranet2Model.csdl(2,2) : error 0010: The element Schema in namespace http://schemas.microsoft.com/ado/2009/11/edm was unexpected for the root element. The expected Schema in one of the following namespaces: http://schemas.microsoft.com/ado/2006/04/edm, http://schemas.microsoft.com/ado/2007/05/edm, http://schemas.microsoft.com/ado/2008/09/edm.

解決方式

安裝 .NET Framework 4.5.x 就可以解決