Posts Tagged Data Services (Astoria)

Přednáška ADO.NET Entity Framework – MFF UK, Praha

Pokud jste nestihli první pražskou nebo brněnskou přednášku, máte v Praze další šanci. 3.11.2009 (úterý) od 17:20 v rámci programátorských večerů na MFF UK.

Více info zde nebo zde, registrace na http://akce.altairis.cz.

Prezentace ke stažení.

Tags: , , , , , ,

What’s new in Entity Framework 4 Beta 2 and ADO.NET Data Services 4 Beta 2

Nice list at http://blogs.msdn.com/adonet/archive/2009/10/19/vs2010-and-net-framework-beta-2-announced.aspx.

Tags: , , , , ,

Firebird(Client) with Silverlight

I had this idea on my list for a couple of months and I was always postponing it, because it’s stupid. But you know. Exploring the unexplored ways, that’s what I like to do :) .

But during (or after, I don’t remember) my presentation about Entity Framework there was a question about accessing the data provided by EF from Silverlight. Sure, doing it directly isn’t a good idea and in fact this is the reason why Astoria, eh, ADO.NET Data Services is here. But is this really stupid? Can it work?

Nope. Not at all. I tried to build FirebirdClient in a Silverlight environment and I failed. I expected to fail after some attempts with modifications and using only subset of features like i.e for Compact Framework, but this was really fast. Silverlight is only subset of .NET Framework. And one crucial part is not there, it’s System.Data stuff and without it you can drop major part of any ADO.NET provider’s code.

If you need data, use ADO.NET Data Services or any other webservices (which will also fit for DDL etc. commands). That also means, that writing Silverlight based Firebird database manager (without any support backend), which is one of few reasonable ideas, cannot be done either.

Tags: , ,

Přednáška ADO.NET Entity Framework – WUG, Brno

20.10.2009 (úterý) od 17:00 budu přednášet o novince (je to ještě novinka, když .NET 4 je za dveřmi?) v .NET 3.5 SP1 – Entity Framework. Tentokrát v Brně. Stejně jako v Praze se podíváme na nové vlastností, které přinese Entity Framework v4 v .NET 4, stejně tak, pokud zbyde čas, přijde na řadu rychlé info o ADO.NET Data Services (Astoria).

Registrace na http://wug.cz/Aktuality/tabid/36/ctl/Detail/mid/492/ItemId/303/language/cs-CZ/Default.aspx.

Prezentace ke stažení.

Tags: , , , , , ,

Přednáška ADO.NET Entity Framework – Microsoft, Praha

13.10.2009 (úterý) od 17:30 budu přednášet o novince (je to ještě novinka, když .NET 4 je za dveřmi?) v .NET 3.5 SP1 – Entity Framework. A nejen to. Zabředneme také do nových vlastností, které přinese Entity Framework v4 v .NET 4. Pokud zbyde čas, na řadu přijde rychlé info o ADO.NET Data Services (Astoria).

Více info, včetně registrace na http://akce.altairis.cz/Events/298.aspx.

Prezentace ke stažení.

Tags: , , , , , ,

Astoria 1.5 CTP2 – projections and counts

CTP2 for Astoria 1.5 has been released some days ago and this version comes with couple of new features. You can read about the list on http://blogs.msdn.com/adonet/archive/2009/09/01/ado-net-data-services-v1-5-ctp2-now-available.aspx. For me, the projections support and count support are the most exciting.

The lack of projections support was a pain if you had some big blob fields in your model. I sketched little workaround here. But since this version you can use the $select operator to get only columns you’re interested in. You’ll write for instance http://foobar/Service.svc/Masters?$select=ID,Name,Details, and you get back object with properties ID, Name and collection Details (for simple master-detail example). So you can easily get data except already mentioned big blob fields.

The other new feature is support for count. You can simply ask Astoria to return number of entities in particular query. You can use it without any filtering etc. i.e. http://foobar/Service.svc/Masters/$count or with other operators as well, i.e. $filter: http://foobar/Service.svc/Masters/$count?$filter=Name eq 'rrr'. So you don’t have to fetch all the data returned just to get the final number. And that’s not all. You can get count of entities in result also inline. With keyword $inlinecount you get the result and the count as well. The query http://foobar/Service.svc/Masters?$inlinecount=allpages&$top=2 will return at most two entities and count of all. On the other hand the http://foobar/Service.svc/Masters?$inlinecount=allpages&$filter=ID lt 5&$top=2 will return count of entities where ID < 5 and only two of these. Simply the $inlinecount is applied to whole result after all $filters have been applied. This inline count is pretty nice especially for classic “list of products” view with paging not to overload the user.

Looking forward to see the new version of Astoria, Astoria “offline” and Entity Framework v4 all released.

Tags: ,

Astoria “offline” and Firebird

Few weeks ago I was playing (and I’m still about to continue playing) and writing about Astoria offline. I setted up some challenge for me to try to make it work with Firebird (as the big database, SQL CE still as local store). It looked like it should be plausible. But it’s not. :)

After creating model from database, creating and rewriting sql scripts for Firebird, I tried to use this model in Astoria offline. Here I hit the wall. After couple of hours I gave up and talked with Pablo Castro, who did much of the work in Astoria offline. He confirmed that there’s no particular check and reject other databases, but with this “offline preview” there’s a lot of assumption for MS SQL Server.

Never mind, when another test version will be available be sure, I’ll try it and we’ll see what’s what. :)

Tags: , , ,

Playing with Astoria “offline”

Astoria so called offline has been released while ago. But until now I have no time and yen for playing with it. When I first heard about offline support for Astoria I was thinking, hmm that’s going to be cool and though that it will be build inside the client itself, with an option to store this information and do offline work when no connection is available – something like “we suppose the connection is mostly available, hence this is only for the few cases when not”.

But not. Astoria offline (at least the preview is based on, of course, regular Astoria and Entity Framework and then Sync Framework.So finally it works like this. You’re doing queries using Entity Framework (local model) and local SQLCE database and if you need to get fresh data and/or push data back, you simply synchronize this source, using Sync Framework, with the webservice. That’s a little bit different from what I was expecting. :)

I was playing with MS SQL Server as the source for webservice (but I’m also planing to use Firebird, but that needs some more hand work). Simple master-detail scenario for start (for conflicts and ordering testing it’s enough).

create table sync_master(id int primary key, foo nvarchar(20) not null);
create table sync_detail(id int primary key, id_master int not null, bar nvarchar(20) not null);
alter table sync_detail add foreign key (id_master) references sync_master(id);

If you have your database ready, you’ll add new model and the service. Currently the alpha preview generates you change script, to add some tracking columns into your tables and you’ll simply run it. Good news is that you’ll also get the script with drops and removes, thus after playing you can put tables back into original shape (but I still prefer playing on separate tables, as during testing I screw a lot of stuff). Configuration of service is more or less the same. Only change you have to do, is to allow synchronization.

(config as IDataServiceConfiguration2).AllowSynchronization = true;

That’s all. The service works as classic Astoria (I still get not used to ADO.NET Data Service name). Now the fun stuff comes. You can add any type of application to use the Astoria offline. I started with simple console application, to introduce as less as possible external screwing inputs.

First problem you may encounter is not working “auto setup” for offline work. I did dozen of apps and didn’t find reliable way to make it work always. Close to this I was with these steps (but still not 100%):

  • recompile the service
  • view it in browser
  • access one entity set
  • hope for the best

Now when you add service reference to you project the additional process kicks in (should) and generates local SQLCE database, the model and some classes. When I was able to make this work, every time I reached the Adding new database file to project... step I get error message:

An error occurred while processing the local data file: 

Exception has been thrown by the target of an invocation.

Clicking OK and ignoring it worked and introduced none (as far as I’m aware of) problems later. No problem, remember it’s alpha preview. With finally all set up, you can start playing.

static void Main(string[] args)
{
	using (testovaciEntities ent = new testovaciEntities())
	{
		sync_master m = ent.sync_master.FirstOrDefault();
		if (m != null)
		{
			Console.WriteLine("Old: {0}", m.foo);
			m.foo = "b";
			Console.WriteLine("New: {0}", m.foo);

			ent.SaveChanges();
		}
		else
		{
			Console.WriteLine("No item");
		}
	}

	using (testovaciEntities ent = new testovaciEntities())
	{
		foreach (var item in ent.sync_master)
		{
			Console.WriteLine("ID: {0} \t Foo: {1}", item.id, item.foo);
		}
	}

	Console.WriteLine("Syncing");
	Sync();
	Console.WriteLine("Done");

	using (testovaciEntities ent = new testovaciEntities())
	{
		foreach (var item in ent.sync_master)
		{
			Console.WriteLine("ID: {0} \t Foo: {1}", item.id, item.foo);
		}
	}
}

static void Sync()
{
	var serviceSync = new DataServiceSyncProvider(new Uri("http://localhost:1744/WebDataService1.svc"), "global");

	var localSync = new ObjectContextSyncProvider(() => new testovaciEntities());
	localSync.ConflictHandler =
		(ISyncRecord sourceChange, ISyncRecord destinationChange) =>
		{
			Console.WriteLine("Conflict");

			return SyncConflictResolutionAction.SourceWins;
			//return SyncConflictResolutionAction.DestinationWins;
		};

	var so = new SyncOrchestrator();
	so.RemoteProvider = serviceSync;
	so.LocalProvider = localSync;
	so.Direction = SyncDirectionOrder.UploadAndDownload;
	so.StateChanged +=
		(object sender, SyncOrchestratorStateChangedEventArgs e) =>
		{
			Console.WriteLine("From {0} to {1}", e.OldState, e.NewState);
		};

	try
	{
		SyncOperationStatistics stats = so.Synchronize();
	}
	catch (Exception ex)
	{
		Console.WriteLine(ex.Message);
	}
}

I was expecting the synchronization to just work, and yep, it does. Anyway I was more interested in some conflicts, ordering etc. Because I was playing with Sync Framework when it was introduced and also done couple of presentations, I wasn’t expecting some problems with ordering.

The conflict resolution is little bit different than in pure Sync Framework, but the idea behind is the same. Simply check SyncConflictResolutionAction enum (there’s no MSDN doc for it right now). The synchronization worked for me as well, with some minor problems. Sometimes the conflict was resolved, but one source kept the old data. Maybe I’m doing something wrong, maybe it needs some support in ResolveSyncConflict on server side too. Never mind, I think in beta it will shine.

OK, that’s pretty much all. It works, have some bugs (but remember it’s alpha preview) and in some scenarios could be really useful. Let’s try the Firebird – Entity Framework (and Astoria) works with Firebird, the Sync Framework too, so how hard can it be …

Tags: , , , ,

ADO.NET Data Service and non-public properties in Entity Data Model

I was punch directly to my face right now. Everytime I’m doing some ADO.NET Data Services (Astoria) speak I’m showing simple models, with almost no modifications. And everything works great. But I found, right now, that if you have entity in your model with property access getter or setter setted to anything except public, the service will not work. Grrr.

I understand that hiding setter causes the updates or inserts to stop working. But why the getter? If I limit my entity set to i.e. AllRead, then the querying should just work. Hmm, it’s probably related to missing select support (in 3.5SP1 as well as 4.0).

Anyway it was really “interesting” ;) to find this out.

Tags: ,

Select method (kind of) in ADO.NET Data Services (to exclude big blob fields)

Current version of ADO.NET Data Services doesn’t support Select method. Neither the .NET Framework v4. This may cause you problems when your entity contains relatively big blob fields, like photo of user in users table/entity. You can do almost nothing with it.

Today I was facing exactly this problem. While inspecting some easy workarounds I found the problem with non-public members in entity. But I came with another solution. It’s not directly solution to missing Select method support, but kind of workaround for blob fields, mainly focused on web usage.

First I deleted the blob column (in my case Photo) from my entity (in my case User). Then I created stored procedure to return this field from database and created Function import. If you’re working with EF4, then the code for invoking the procedure is generated for you, in EFv1 you have to create it yourself as the procedure returns non-entity result. For this method I created Service Operation method with WebGet and SingleResult attributes, plus MimeType attribute. The method I returning directly byte[] (from .NET 4 you can directly return primitive types).

Sure, this field must be nullable or has some default value to support insert. For update the blob field you have to write another separate method. Yep, some manual work. ;)

Removing the field from EDM (remember you can have more models in you app, hence you don’t have to change your model for desktop app for example) has some drawbacks and limits you. On the other hand, creating the separate method has some advantages. With all these attributes declared, you can call http://something/Service.svc/GetUserPhoto/$value?id=x and get the result back directly, with proper mime type. So you can easily use it in <img /> tag without any further processing (yes, with you could do http://something/Service.svc/Users(x)/Photo/$value, but that means not deleting the “Photo” property from model and when asking for entity getting all the binary data to the client, which is what I’m trying to avoid).

It’s not the best solution one can imagine, sure. But works for selecting as well as updating/inserting, with only small limitation and small amount of extra work.

Here’s the complete code, if you wanna try (no updating/inserting support, from EDM just Photo deleted):

[MimeType("GetUserPhoto", "image/jpeg")]
public class SelectTest : DataService<TestEF>
{
	public static void InitializeService(IDataServiceConfiguration config)
	{
		config.SetEntitySetAccessRule("*", EntitySetRights.All);
		config.SetServiceOperationAccessRule("GetUserPhoto", ServiceOperationRights.All);
	}

	[WebGet]
	[SingleResult]
	public byte[] GetUserPhoto(int id)
	{
		return this.CurrentDataSource.GetUserPhoto(id).FirstOrDefault();
	}
}
create table Users (ID int primary key, FirstName nvarchar(255) not null, LastName nvarchar(255) not null, Photo varbinary(max));
go

create procedure GetUserPhoto(@ID int)
as
begin
  select Photo from Users where ID = @ID;
end
go

Tags: ,

Firebird ADO.NET Data Provider 2.5.0 Beta 1 for .NET 3.5/2.0 [with Entity Framework support]

I’m pleased to announce 2.5.0 Beta 1 for .NET 3.5/2.0 [with Entity
Framework support] version. It contains several bugfixes as well as
brand new Entity Framework support (still beta!). Looking forward to
your feedback.

Binary as well as sources can be downloaded from
http://www.firebirdsql.org/index.php?op=files&id=netprovider.

Tags: , , , , , , ,

gteq and lteq problem in ADO.NET Data Services

If you’re looking to some articles/resources/documents you may find, that “greater than or equal” or “lower than or equal” operators have keywords gteq or lteq. But when you try these, the request fails.

Well, the simple reason is, that these operators are in fact ge or le. Hope this helps you save some time when trying to find out what’s wrong.

Tags: ,

Entity Framework a ADO.NET Data Services

Tak a je to. Entity Framework a ADO.NET Data Services budou součástí SP1 pro VS2008 a .NET 3.5. Paráda. Doufám, že do té doby dokončím i podporu pro Entity Framework v ADO.NET provideru pro Firebird.

Tags: , , , , ,