WCF RIA Services Support for EF 4.1 (and EF Code-First)

I am pleased to announce that WCF RIA Services now supports EntityFramework 4.1 (including EF Code-First). This will be added to the WCF RIA Services Toolkit, but we are releasing it today as a NuGet package.

Background

In April 2011, EntityFramework released version 4.1 of their product, which contained 2 important updates –

  1. DbContext based API
  2. Code-First support

You can get more information about the EF 4.1 release on the ADO.NET team blog. We are adding support to these 2 features in the WCF RIA Services V1.0 SP2 release. This consists of the following additions / changes –

  • A new domain service that supports DbContext and Code-First known as DbDomainService.
  • Tooling support to generate code for the custom DbDomainService, just like the LinqToEntitiesDomainService.

This will allow you to use EF DbContext in either the data first, model first or Code First modes with WCF RIA Services.

Download

Pre-requisites

    – You will need WCF RIA Services V1.0 RTM, SP1 or SP2 installed.

NuGet package – The WCF RIA Services runtime support for EF 4.1 is in the assembly Microsoft.ServiceModel.DomainServices.EntityFramework.dll. You can get it here-

RIAServices.EntityFramework NuGet Package

DbDomainService CRUD Methods Code Snippet – For the benefit of anyone trying this preview, I have put together code snippets that will generate CRUD methods for the custom DbDomainService. Please note that these code snippets will not be released/supported officially. They are there only to make the process of trying out this preview as easy as possible. You can download the code snippets here –

C# Snippet / VB Snippet

What is not included?

1. Wizard support – The wizard support for DbDomainService is embedded in the product and cannot be added until we are ready with the next release. For now, you will have to manually code a DbDomainService or use the Code Snippets provided

2. Database Initializer settings – The EF Database Initializer needs to be tweaked depending on whether the context is runtime or design-time. This logic is also in the product and not available as a part of this preview. So for this preview, you have to set the initializer manually for design-time as I explain in the walkthrough below.

Getting started

Here I will walk you through how you can get started with using EF Code-First in a WCF RIA Services App. Let us assume that you have WCF RIA Services installed. If not, you can install it from here.

1. Create a WCF RIA Services Application

Create a Silverlight Application with the Enable RIA Services option enabled or any other WCF RIA Services project (like the Silverlight Business Application).

2. Add the RiaServices.EntityFramework NuGet package to the project.

To do that, go to Package Manager Console and type “Install-Package RIAServices.EntityFramework”.

This should automatically install the EntityFramework 4.1 for you, if you don’t have it already installed. If you already have it installed, add a reference to EntityFramework.dll to the project. You will also need to add a reference to System.Data.Entity.dll.

3. Add an EntityFramework Model using the Code-First approach to the web project.

So let us define an entity called Product, with some properties on it. We will also define a  DbContext called ProductDbContext that has a DbSet of Products on it. (You can find more information on how to use EF Code-First here). So the code looks like this –

namespace EFCFSample.Web
{
    public class Product {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int? CategoryID { get; set; }
        public Decimal? UnitPrice { get; set; }
        public bool Discontinued { get; set; }
    }

    public class ProductDbContext : DbContext {
        public ProductDbContext()
        {
            if (HttpContext.Current == null)
            {
                Database.SetInitializer<ProductDbContext>(null);
            }
        }

        public DbSet<Product> Products { get; set; }
    }
}

Notice the code I have in the ctor of the ProductDbContext? That piece of code checks if the HttpContext is null and if it is, sets the database initializer to null. Here is why we need it.

Additional Code Changes Needed to Work with the Preview –

EntityFramework initializes the database when you create a DbContext. instance. You want that to happen at runtime but not at design time. WCF RIA Services will initialize the DbContext at design time as well (only for EF Code-First). So we need to prevent the database from being initialized at design time. The logic to to do this is embedded in the product that will be released with the RIA Services V1.0 SP2.

So, until then, you will have to manually prevent the database from being initialized at design time. To do that, we identify if the execution context is design time context (by checking if the HttpContext.Current is null) and if it is, we set the database initializer to null. Note that this will not be required with the actual release bits of WCF RIA Services V1.0 SP2 and Toolkit.

4. Add a DbDomainService.

Alright, now that the DbContext has been set up, we can go ahead and define a class deriving from DbDomainService<ProductDbContext> (similar to any other RIA Services DomainService) like this –

namespace EFCFSample.Web
{
    [EnableClientAccess]
    public class DomainService1 : DbDomainService<ProductDbContext>
    {
    }
}

5. Add CRUD Methods to the DbDomainService

Now, we will use the C# code snippet to generate the CRUD methods for us. So download the code snippet to the disk. Then use the VS Code Snippet Manager to import it to Visual Studio. For more information on how to do that, refer to this msdn documentation. I would recommend importing it to My Code Snippets (which will be typically under c:\Users\<username>\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets)

When that is done, from within the class declaration, use the shortcut “dbcrud” to get your DbDomainService CRUD methods defined for you.

Note that this code snippet has 3 variables that you need to supply –

  1. Name of the Entity (in this case, Product)
  2. Name of the DbSet property corresponding to the entity (Products)
  3. Name of the entity (this is used to generate the names of the CRUD methods – like GetProducts, etc.).

So our DbDomainService looks like this-

namespace EFCFSample.Web
{
    [EnableClientAccess]
    public class DomainService1 : DbDomainService<ProductDbContext>
    {
        public IQueryable<Product> GetProducts()
        {
            return this.DbContext.Products;
        }

        public void InsertProduct(Product entity)
        {
            DbEntityEntry<Product> entityEntry = this.DbContext.Entry(entity);
            if ((entityEntry.State != EntityState.Detached))
            {
                entityEntry.State = EntityState.Added;
            }
            else {
                this.DbContext.Products.Add(entity);
            }
        }

        public void UpdateProduct(Product currentProduct)
        {
            this.DbContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct), this.DbContext);
        }

        public void DeleteProduct(Product entity)
        {
            DbEntityEntry<Product> entityEntry = this.DbContext.Entry(entity);
            if ((entityEntry.State != EntityState.Deleted))
            {
                entityEntry.State = EntityState.Deleted;
            }
            else {
                this.DbContext.Products.Attach(entity);
                this.DbContext.Products.Remove(entity);
            }
        }

    }
}

Again, the SP2 release bits will have full wizard support for DbContext. So you should not need to do any of this dance with the code snippet. But till then, this should be easy enough for you to do.

5. Use the DbDomainService in your App

With this, you have a RIA Services DomainService talking to a EF Code-First database that you can use, just like in any other RIA Services application. Of course, you can add/modify the methods in the generated DomainService or add more CRUD methods for other entities in your EF Model.

In short, your EF Code-First model is ready for use in the WCF RIA Services app! 

If you are interested in trying using EF Code-First or simply DbContext with WCF RIA Services, try out this preview and let us know how you find it. Any feedback from you will be highly appreciated!

About varunpuranik
I am a Developer at Microsoft. I work in the App Server Group on WCF RIA Services.

76 Responses to WCF RIA Services Support for EF 4.1 (and EF Code-First)

  1. Pingback: RIA Services

  2. calanus says:

    Nice one varunpuranik! I already built my database as been waiting a few weeks for you to get this out the door so I automagically generated my entities using the EF Power Tools CTP. Then got a skeleton domain service up and running by creating a throwaway ria solution and generating it from the db the ye olde way (needs a bit of hacking though as dbdomainservicecontext replaces the old LinqToEntitiesDomainService class).

  3. bhugot says:

    Hello,

    I have the following error when compiling the client project.

    Failed to get the MetadataWorkspace for the DbContext type

    For information I have 1 project for the models 1 project for the DbContext and 1 for the DomainService.

    Any Idea?

    Thanks.

    • Jeff Handley says:

      @bhugot – We will try to reproduce this. Is there anything unusual about your DbContext model?

    • Jeff Handley says:

      @bhugot,

      We haven’t been able to reproduce this error yet, and we’re also using models in 1 project for the DbContext and another project for the DomainService.

      Any other details you can provide would be much appreciated.

      Thanks,
      Jeff

      • bhugot says:

        I created a sample project with the same class this post sample and i have the same error.

        If I can send you the sample project.

      • Jeff Handley says:

        Ick. Can you let me know which versions of the following you are using?

        – Visual Studio 2010 — RTM or SP1?
        – WCF RIA Services V1.0 — RTM, SP1, or SP2 Preview?

      • bhugot says:

        VS SP1 and RIA SP1

      • bhugot says:

        I think I found the problem, it’s database connection string.

        [ArgumentException: Keyword not supported: ‘data source’.]
        System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +5055124
        System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +98
        System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) +118
        System.Data.EntityClient.EntityConnection..ctor(String connectionString) +81
        System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name) +591
        System.Data.Entity.Internal.LazyInternalConnection.Initialize() +85
        System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() +31
        System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +249
        System.Data.Entity.Internal.InternalContext.Initialize() +30
        System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext() +31

      • bhugot says:

        Ok it was my connection string,

        I was using

        And now i use

      • bhugot says:

        Ok I need to put the connectionstring in my dbcontext constructor for the splitted version.

        Seems my app.config is not used.

        “Server=XXXX;DataBase=BDDRH_DATA_DEV;Trusted_Connection=True”

      • Dan says:

        Hey guys,

        I had this same problem too. Got around it by fixing up my connection string from the one that was created from Code-First Reverse Engineer.

        Cheers,
        Dan

    • joshmouch says:

      I got the MetadataWorkspace error quite a bit while developing my Silverlight application. Here’s what I did to resolve the error:

      Code first and WCF RIA services: Failed to get the Metadataworkspace for the DbContext type ‘{Type}’

  4. Pingback: The RIA Services Blog

  5. Rudi Larno says:

    Great, just what I needed, and when I need it 😉

    Any chance yet that someone has created a scaffolder for the DomainService?

    I was thinking that in a similar manner like the MvcScaffolding one could create a scaffolder that generates the CRUD methods for a Type, replacing the need for the snippets

  6. admin says:

    This is a wonderful piece of info. I have waited patiently for this bit.RIA Services EF and Code First ROCKS! Thanks and keep it coming.

  7. Pingback: RIA Services EF Code First Support

  8. admin says:

    Hello,

    I did a sample by following this tutorial and when I compiled I got the following error.

    Failed to get the MetadataWorkspace for the DbContext type

    I am using VS 2010 and RIA SP2

    I can see other people are reporting same error.

    Any Guidance will be appreciated.

    Thanks.

    • varunpuranik says:

      Hello,
      This error is most likely because your EF CodeFirst database is not configured properly – either the connection string or SQL Express on your machine (which EF uses by default), or some other setting might be wrong. Could you try using your EF CodeFirst database in a console app as described here? If that works, fine please let me know and I will try to help you figure out what is going on with your app.
      Thanks!

  9. Pingback: RIA Services EF Code First Support – www.nalli.net

  10. bhugot says:

    Hello,

    I would like to remove all references to EntityFramework and RiaService Dll’s from my Models project. I still need one reference because I need to use IncludeAttribute. Is there a way to configure DomainService to always consider the IncludeAttribute is on Association.

    Thanks

    • Jeff Handley says:

      The reference to the DomainServices assembly is required for the IncludeAttribute, unless you take a drastically different approach with metadata, using a custom metadata provider.

    • ET says:

      I second this request. There has to be a simple way of adding RIA aspect on top of existing Domain Model. The attribute is utterly useless unless I’m defining the model in the RIA project for the sake of RIA service and who does stuff like that these days?

  11. Pingback: Microsoft Weblogs

  12. Pingback: OPC Diary » Blog Archive » WCF RIA ServiceでのCode Firstサポートに関するメモ書き

  13. Mark Thompson says:

    The link to the C# code snippet is not working for me. It redirects me to:http://code.msdn.microsoft.com/site/search?f%5B0%5D.Type=Affiliation&f%5B0%5D.Value=Official&f%5B0%5D.Text=Microsoft&f%5B1%5D.Type=SearchText&f%5B1%5D.Value=RIA%20Services&sortBy=AuthorAsc&pageIndex=1

    and I don’t see the snippets in the list. Any suggestions?

    • Rudi Larno says:

      Mark,

      would you mind trying the scaffolding, it provides similar code to the snippets, for C#

      See http://larud.net/Blog/archive/2011/07/04/simple-wcf-ria-services-ef-4-1-scaffolding.aspx

      Any comments on it are really welcome.

      • Mark Thompson says:

        Rudi,

        I actually did use it and it seemed to work well however the DomainService was expecting INSERT, UPDATE and DELETE methods on the DbContext which did not get generated automatically during the process. I was expecting, perhaps incorrectly, that the DbContext would have automatically generated those during the ‘SCAFFOLD DBCONTEXT’ command. That’s what prompted me to ask about the code snippets thinking that perhaps the code snippets would create them. (I have some stubs in place for now that resolve the compile errors.)

        Great work with the NuGet package and the scaffold. Thanks for that. Other than the issue described above (which may just be my problem) it worked well. The only suggestion I would have is related to the article. You included a screenshot of the generated domain service but not of the generated dbcontext. That would be helpful particularly in this case since my dbcontext doesn’t have what I’m expecting.

      • Rudi Larno says:

        Hi Mark,

        The package should have included a DbContextExtentions.cs class in the \EntityFramework folder. This class has the Insert/Update/Delete extension methods for the DbContext.

        Those methods are taken from Remco Blok/Arthur Vickers to make the CRUD methods even simpler.see http://social.msdn.microsoft.com/Forums/en-US/adonetefx/thread/57793bec-abc6-4520-ac1d-a63e40239aed/

        Contact me via @rlarno or via mail: Rudi dot Larno at euri.com

      • Mark Thompson says:

        Rudi,

        I deleted the existing code and ran through the steps again and everything generated as you described. I must have missed a step the first time.

        Thanks for your help.

  14. Adam Carter says:

    I’m getting a reference not found error on “DbDomainService”. What am I missing?

  15. Mark Thompson says:

    For anyone that might still be struggling with the "Failed to get the MetadataWorkspace for the DbContext" error and you are certain that the connection string is correct, verify that your class includes an ID property.

    The class I was working with should of inherited the Id but wasn’t which was causing me the problem. Ensure that any class that will be included on the client includes an ID either directly or via inheritance.

    The ID:

    – Has to be a public property (cannot be a public member)
    – Must include both getter and setter
    – Name is case-insensitive
    – Datatype worked as any type I tried (int, string, Guid, double). I’m using int.

    Hope that helps.

  16. Hello,

    Any guidance/code on how to incorporate this with AuthenticationBase ?

    Thank you,
    Michael

  17. Remco Blok says:

    Hi Varun,

    First of all congrats and thanks very much on making the EF 4.1 DbContext and Code First support for RIA Services available. It’s absolutely awesome!

    I posted a sample on code.msdn where I was attempting to introduce Unit-Of-Work and Repository patterns. To use an interface instead of a DbContext derived class directly my domain service had to derive from the DomainService class directly of course instead of from DbDomainService. I would decorate the domain service with DbDomainServiceDescriptionProvider(typeof(BookClubContext)) to allow RIA Services to do its magic, but otherwise I would try to use an interface instead of the DbContext type directly.

    I did end up trying to create a base domain service class similar to DbDomainService but one that could work with an interface instead of a DbContext, but I am still struggling with separating out some of the concerns/dependencies to do with handling DbUpdateConcurrencyExceptions as you can see by the fact I cast to DbContext and IObjectContextAdapter. I would love to hear from you if you can provide further guidance.

    To make the base domain service class generic I would have to specify a constraint on the generic type to have it implement an interface like System.ComponentModel.IChangeTracking (as well as IDisposable), but DbContext does not implement this interface and I am somewhat reluctant to introduce a similar interface thinking that in a solution you’re likely to have only a single DbContext that can be used by multiple domain services so that you may not need a generic base domain service class, and a non-generic base domain service class may do.

    The sample can be found here: http://code.msdn.microsoft.com/Task-based-MVVM-Sample-for-2eb86fab.

    many thanks

    Remco

    • Remco Blok says:

      Hi Varun,

      I just updated my sample on code.msdn as I found a way to make a generic UnitOfWorkDomainService as follows:

      public abstract class UnitOfWorkDomainService : DomainService where TContext : IUnitOfWork

      where

      public interface IUnitOfWork : IDisposable
      {
      int SaveChanges();
      }

      I can then create my BookClubDomainService as follows:

      [EnableClientAccess()]
      [DbDomainServiceDescriptionProvider(typeof(BookClubContext))]
      public class BookClubService : UnitOfWorkDomainService

      The issue with the handling of DbUpdateConcurrencyException having a dependency on IObjectContextAdapter in UnitOfWorkDomainService remains for now though.

      kind regards

      Remco

  18. harsmakenibeiro says:

    Hi, I’m having the most strange issue when using the Ria Services. My DbContext is not working anymore. It creates the database, but it does not execute the Seed method, and even if I insert the data manually at the database, it won’t read it. Has anyone seen this happen??
    The code in my DbContext is this:
    public NimbusDB()
    : base(“Server=(local);DataBase=NimbusDB;Trusted_Connection=True”)
    {
    if (HttpContext.Current == null)
    Database.SetInitializer(null);
    else
    Database.SetInitializer(new DropCreateDatabaseAlways());
    }

    • varunpuranik says:

      I am not sure what you want to do. Using Database.SetInitializer(new DropCreateDatabaseAlways()); will cause the default seed method to be used (which will not add any data to the database).
      If you want to insert data using the seed method, you will have to derive from the DropCreateDatabaseAlways type and override the seed method that adds the data to the DbContext.

      • Actually, I was just being stupid. My seed method had an primary key issue, so it rolled back all the data inserted when the error raised. I corrected it and it’s ok now.
        But thanks anyway.

  19. Pingback: WCF Ria Services con Entity Framework 4.1 « Emanuele Filardo

  20. Jobi Joy says:

    Hi Varun, Just wondering how I can take advantage of EF Codefirst on a WCF app and that being used in a WPF application. I am seeing some hints about WCF DataServices exposing DbContext. But I couldnt locate any proper sample or reference.
    Another thing I was seeing in your blog is exposing DbDomainService, but can I use that in a typical WPF app so that this DbDomainService take care of the EF changes back to the DB.

    Thanks in Advance
    Jobi

  21. Rodd says:

    I’m kind of new to EF and RIA so bear with me if this question is a bit simple. Does this new release of the RIA Toolkit mean that RIA is able to build its client side model from the definitions in the fluent api?

    IOW, does this mean I no longer need attributes like [Association()], [Include] and [Composition] on my POCO objects and that RIA will use things like modelBuilder.Entity().HasMany(p => p.Address) to determine relationships?

    • varunpuranik says:

      Yes, if you have built your model using EF CodeFirst (and using fluent api), then WCF RIA Services will be able to figure out the relationships and other attributes from the model and that will reflect on the client side as well.

  22. bhugot says:

    hello I have a problem with DisplayAttribute when using a RESX in another project.

    Here are my projects

    _Test.Resources.Web (NET4.0) ( the RESX)
    _Test.Resources (SILVERLIGHT) (the RESX via project link)
    _Test.Ria.Web (NET4.0) ( the DomainService )
    _Test.Ria (SILVERLIGHT)
    _Test.Ria.Context (NET4.0) (the DBContext)
    _Test.Ria.Models (NET4.0) (the Entitites and metadata)

    When I compile i get “Failed to get the MetadataWorkspace for the DbContext type”

  23. admin says:

    The ‘Failed to get the MetadataWorkspace for the DbContext type’ Error is driving me mad, I have done all the Fixes suggested by others above with no result. My connection string is correct. The funny thing is that the same DbContext works outside of RIA Services.
    What more can I do and I hate to go back to model first (but it works fine).

    • bhugot says:

      I found the fix for my problem, maybe it will fix yours.

      If your models are in another assembly than the DomainService, you need add each ref from the models assembly to the DomainService assembly.

      In my example I had to add the Test.Resources.Web assembly to Test.Ria.Web

      Hope this can help,

      • Uwascan says:

        Yes my models are in another assembly with the proper references added to the DomainService assembly. I dont think the project will even compile without such reference.
        I think the problem has to do with the inability of the library to correctly get metadata info from the DomainService, I am using MySql 5.1 FluentMetadata Provider from Nikil Kotari.
        Every thing works fine when using Model First Approach. I am sticking with Model First form now.
        Thanks for helping.

    • varunpuranik says:

      Sorry about the experience you are having. We will try to improve our experience in our future releases.
      The difference between EF CodeFirst stand alone and with RIA Services is that we initialize a new DbContext at design time as well. One thing you can try is, in the stand alone scenario (withour RIA Services), initialize a DbContext with the default ctor and access the MetadataWorkspace property form it. That might tell you what the actual error is.

  24. hugues says:

    I tried code first with RIA Services and if I don`t have an association between two POCO class objects, it works. But if a POCO A object contains a POCO B object, Silverlight project compilation returns me an error: `”Unable to retrieve association information for association. Only models that include foreign key information are supported.” What is the solution to that?

    public class A
    {
    public int Id { get; set; }
    public string Name { get; set;}
    public B MyB { get; set; }
    }

    public class B
    {
    public int Id { get; set; }
    public string Name { get; set;}
    }

  25. Sanyi says:

    Hi,
    I added the EF Model using the Code-First to e separated class library project.and when I tried to build it gave me this error:

    Failed to get the MetadataWorkspace for the DbContext type ‘Test.Server.Data.TestContext’. Test.Client

    Could you help me?

  26. Hi,
    Another issue with the “Failed to get the MetadataWorkspace for…”

    In my case, we are doing the build of our software on a separate machine which must not have a copy of our database on it. Because of the issue with RIA services creating an instance of the DbContext in compile-time, we get this error.

    I tried setting the database initializer to null (and I can see that it is really done, I’ve added some textfile logging), but still the same result.

    How can I disable the compile-time instance creation?

    Best regards / Jon

  27. Daniel says:

    I downloaded the WCF RIA SP2 RC offline installer as I wouldn’t want to go online to add a reference to class libaries and subsequent projects. But I couldn’t find the DbDomainService. I have EF 4.1 Update. Is DbDomainService only availabe via NuGet only or an oversight in the offline installer or probably I am missing something. Thank You

  28. Pingback: RIA Services

  29. Jayraj says:

    Dear Friends,
    I have created one Silverlight sample application with the help of above mentioned steps.
    I have done with crud operations on single entity. But not able to perform transaction on parent child relations.

    I have Below entities
    Entities:
    1. Order
    a. OrderID PK
    b. OrderDate

    2. Order_details
    a. OrderDetailID PK
    b. OrderID FK
    c. Details

    I am facing Issues while
    1. Retrieving order with orderdetails
    2. Wanted to update both entities at a time. (Parent Child)

    It will be better if anyone provide me steps to solve these issues
    Please provide the solution on my issue.

    Thank You

    • varunpuranik says:

      Hi Jayraj,

      First of all, I don’t think you have to do anything special for Composition scenarios (like the one you have above) as far as RIA Services EF CodeFirst support goes (in other words, RIA Services doesn’t treat Composition scenarios any differently for EF CodeFirst).

      In your model above, you need to add a collection of OrderDetails to the Order entity and an Order to the Order_details entity. So you would have something like this:

      public class Order
      {
      public int OrderID { get; set; }
      public Nullable OrderDate { get; set; }
      Include]
      public virtual ICollection Order_Details { get; set; }
      }

      public partial class Order_Detail
      {
      [Key]
      public int OrderID { get; set; }
      public string Details { get;set; }

      ForeignKey("OrderID")]
      public virtual Order Order { get; set; }
      }

      You also need to include Order_Details when you get the Order, to solve both the issues #1 and #2 you mentioned above. So your GetOrders method in the DbDomainService should look like this:
      public IQueryable GetOrders()
      {
      return this.DbContext.Orders.Include("Order_Details");
      }

      If you want more help on Compositions in RIA Services, you can refer to this blog post.

      Hope this helps,
      Varun

      • Jayraj says:

        Hi varun,

        Thanks a lot;
        its working now. But sometime system throws error 4004, I didn’t understand this error why it comes.

        Thanks
        Jayraj

  30. Jayraj says:

    Dear Friends,

    I am trying to download Large document (300+ MB PDF Files) using Silverlight + RIA Services.

    Can anybuddy help me on this?

    • varunpuranik says:

      Can you elaborate on the problem you are facing? What exactly is your scenario?

      • Jayraj says:

        Hi Varun,

        I have created Silverlight application, and added one service web project. Write the download code in service. I am trying to download file using filestream. I debug the code, and found It read all the bytes, and write to disk, but after download complete when i going to open that pdf file; it not open. I am trying to download pdf file of size 384 MB. Again performance issue is there, I need the solution for that also.

        I am not sure the way of implementation is correct or not. So please suggest me best way to implement this functionality.

      • varunpuranik says:

        Hi Jayraj,

        I am not sure WCF RIA Services is the right approach for your problem. You might be better off using core WCF. Check this as a starting point.

        Hope this helps,
        -Varun

  31. I’m also struggling with the “Failed to get the MetadataWorkspace…” -error. I know the reason why I get it which is because I don’t have working database connection on build time. I have set the DbContext initializer to null, but it still inspects the database. Is it possible to compile EF Code First + RIAServices projects without working database connection?

  32. bhugot says:

    Hello,

    When can we expect to have this for EF4.2?

    Thanks.

  33. Cindy K. says:

    You mention in this post that since this is only a preview release, you provided some code snippets (above) to “make the process of trying out this preview as easy as possible”, but that they will not be released/supported officially. Now that WCF RIA Services SP2 is actually out, is there a different way to do this built in to the final release, or are these snippets still the only way to use this feature?

    Is there a user guide for the Toolkit that walks us through how to use the various features, even if we don’t have a lot of experience with RIA Services in general?

    Thank you!

  34. Justin says:

    I have a question regarding the code-gen:

    The ‘Insert’ method checks if the entity is attached or adds it if necessary:

    DbEntityEntry entityEntry = this.DbContext.Entry(myEntity);
    if ((entityEntry.State != EntityState.Detached))
    {
    entityEntry.State = EntityState.Added;
    }
    else
    {
    this.DbContext.MyEntities.Add(myEntity);
    }

    The ‘Delete’ method check if the entity is marked as deleted or else marks as deleted if necessary.

    DbEntityEntry entityEntry = this.DbContext.Entry(myEntity);
    if ((entityEntry.State != EntityState.Deleted))
    {
    entityEntry.State = EntityState.Deleted;
    }
    else
    {
    this.DbContext.MyEntities.Attach(myEntity);
    this.DbContext.MyEntities.Remove(myEntity);
    }

    Shouldn’t the ‘Delete’ method be checking if the entity is attached instead? In other words:

    DbEntityEntry entityEntry = this.DbContext.Entry(myEntity);
    if ((entityEntry.State != EntityState.Detached))
    {
    entityEntry.State = EntityState.Deleted;
    }
    else
    {
    this.DbContext.MyEntities.Attach(myEntity);
    this.DbContext.MyEntities.Remove(myEntity);
    }

    Thanks

  35. Deepak says:

    Great job @Varun.
    I want to support mocking for unit testing, currently when I see the following code, it limits the
    caller to pass entity framework DBContext so I can’t do mocking. Can you suggest how to achieve that?
    public class DomainService1 : DbDomainService

  36. Pingback: Wi Fi Horizon, use copoun inside

  37. Pingback: Sports Betting Champ Maximizer

Leave a reply to Deepak Cancel reply