SaveChanges has no effect when saving data using Data Services

Let’s use Dynamic CRM Data Service as example here. Let say you have an existing product data and you want to update it. You do the following.

var result = from r in ctx.ProductSet where r.ProductId == productid select r;

updateProduct = result.FirstOrDefault();

updateProduct.ProductId = newProductId;

 

ctx.UpdateObject(updateProduct);

ctx.SaveChanges();

 

I will find no error but nothing updated. Do the follow instead.

var result = from r in ctx.ProductSet where r.ProductId == productid select r;

result.FirstOrDefault().ProductId = newProductId;

 

ctx.UpdateObject(result.FirstOrDefault());

ctx.SaveChanges();

 

This should work now. Both of the code should work but somehow this first one will not.

Posted in .Net | Tagged , , | Leave a comment

Row counts cannot be requested when the MaxProtocolVersion of the data service is set to DataServiceProtocolVersion.V1

You might have a service reference like

public CRMServiceReference.CRM13Context ctx = new CRMServiceReference.CRM13Context(new Uri(crmConnection));

Then you do a LINQ query

var result = from r in ctx.ProductSet where r.ProductNumber == ProductNumber select r;

if (result.Count() == null) {}

Then you hit the error message:

Row counts cannot be requested when the MaxProtocolVersion of the data service is set to DataServiceProtocolVersion.V1

I really do not think the version number matter and the error is meaningful. Somehow you can use this workaround.

if (result.FirstOrDefault() == null) {}

This work for me for now.

Posted in .Net | Tagged , | Leave a comment

How to retrieve a subset of rows from subquery using LINQ

 I have spent hours to get the LINQ query on how to retrieve a subset of rows from subquery using LINQ.

How I mean is not a single return like this:

var queryGroupMax =

        from student in students

        group student by student.Year into studentGroup

        select new

        {

            Level = studentGroup.Key,

            HighestScore =

            (from student2 in studentGroup

             select student2.ExamScores.Average()).Max()

        };

The subquery about only return a single Max() value.

What I mean by subquery is those that returns many rows, like:

SELECT * FROM  [Products] WHERE [ProductID] IN (SELECT ProductID FROM [dbo].[Order Details])

Like the subquery above SELECT ProductID FROM [dbo].[Order Details] returns all the ProductID from the Order Detail Table.

I never found any correct example out there. I downloaded Linqer, somehow the Linqer giving me the almost correct answer.

var result = from Products in db.Products where (from Order_Details in db.Order_Details
select new{Order_Details.ProductID}).Contains(new { ProductID = ()Products.ProductID })
select new {  Products.ProductID,
Products.ProductName,
Products.SupplierID,
Products.CategoryID,
Products.QuantityPerUnit,
Products.UnitPrice,
Products.UnitsInStock,
Products.UnitsOnOrder,
Products.ReorderLevel,
Products.Discontinued
};

I somehow managed to sweat it through to modify if without the (). Tested and it works.

var result = from Products in db.Products where (from Order_Details in db.Order_Details
select new{Order_Details.ProductID}).Contains(new { ProductID = Products.ProductID })
select new {  Products.ProductID,
Products.ProductName,
Products.SupplierID,
Products.CategoryID,
Products.QuantityPerUnit,
Products.UnitPrice,
Products.UnitsInStock,
Products.UnitsOnOrder,
Products.ReorderLevel,
Products.Discontinued
};

Posted in .Net | Tagged , | Leave a comment

Cannot see Snapshot in Hyper Manager

Click on the Snapshot bar.

 snapshot

After that you should be able to see it.

snapshot1

Posted in .Net, Operating System | Tagged , | Leave a comment

How to search null value in Microsoft SQL Server column

A lot of users think they do not have a null value in their column because the do the following.

DON’T

SELECT TOP 1000 [ProductID]

      ,[ProductName]

      ,[SupplierID]

      ,[CategoryID]

      ,[QuantityPerUnit]

      ,[UnitPrice]

      ,[UnitsInStock]

      ,[UnitsOnOrder]

      ,[ReorderLevel]

      ,[Discontinued]

  FROM [northwind].[dbo].[Products] WHERE ProductID = NULL

 

DO

SELECT TOP 1000 [ProductID]

      ,[ProductName]

      ,[SupplierID]

      ,[CategoryID]

      ,[QuantityPerUnit]

      ,[UnitPrice]

      ,[UnitsInStock]

      ,[UnitsOnOrder]

      ,[ReorderLevel]

      ,[Discontinued]

  FROM [northwind].[dbo].[Products] WHERE ProductID IS NULL

 

Remember don’t use = NULL but use IS NULL but not isnull.

Posted in .Net | Tagged , , , | Leave a comment

Visual Studio “14” CTP 3 Released

Visual Studio 14 CTP 3 Released this week. More information here http://blogs.msdn.com/b/visualstudio/archive/2014/08/18/visual-studio-14-ctp-3-released.aspx. There is a new .NET Framework vNext. You cannot download the .NET Framework separately at this moment but to install Visual Studio 14 CTP 3.

For me, still using VS 14 CTP 2.

Posted in .Net | Tagged , | Leave a comment

Feature broken after install VS 14 CTP 2 with Visual Studio 2012

I have Visual Studio 2012, Visual Studio 2013 and Visual Studio 14 CTP 2. Today I realized that I can no longer create Entity Framework in Visual Studio 2012 but I can still do that in Visual Studio 2013.

Symptom: The Entity Data Model Wizard will pop up then disappear.  

Posted in .Net | Tagged , , | Leave a comment