Skip to content

Instantly share code, notes, and snippets.

@FransBouma
Last active October 7, 2015 13:54
Show Gist options
  • Save FransBouma/731a7d904630084c4218 to your computer and use it in GitHub Desktop.
Save FransBouma/731a7d904630084c4218 to your computer and use it in GitHub Desktop.
EF to RavenDB with almost no work. Still have to tell RavenDB that the field with the Key attribute is the Id field, but that's minor.
[TestFixture]
public class SimpleTests
{
private IDocumentStore _store;
[TestFixtureSetUp]
public void Init()
{
InterceptorCore.Initialize("EF6");
System.Data.Entity.Database.SetInitializer<NWDocDBDataContext>(null);
_store = new DocumentStore() {Url = "http://localhost:8080/", DefaultDatabase = "Northwind"};
_store.Initialize();
}
[Test]
public void InsertOrders()
{
// first fetch them from the DB using EF
var ctx = new NWDocDBDataContext();
var alfkiOrders = ctx.Orders.Where(o=>o.Customer.CustomerId == "ALFKI").ProjectToOrderDoc().ToList();
// store them into the doc db
using(var session = _store.OpenSession())
{
foreach(var d in alfkiOrders)
{
session.Store(d);
}
session.SaveChanges();
}
}
[TestFixtureTearDown]
public void Shutdown()
{
_store.Dispose();
}
}
@FransBouma
Copy link
Author

//------------------------------------------------------------------------------
// <auto-generated>This code was generated by LLBLGen Pro v5.0.</auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Documents.Persistence
{
    /// <summary>Static class for (extension) methods for fetching and projecting instances of Documents.DocumentClasses.OrderDoc from / to the entity model.</summary>
    public static partial class OrderPersistence
    {
        /// <summary>Extension method which produces a projection to Documents.DocumentClasses.OrderDoc which instances are projected from the 
        /// results of the specified baseQuery, which returns Northwind.Adapter.EntityClasses.Order instances, the root entity of the derived element returned by this query.</summary>
        /// <param name="baseQuery">The base query to project the derived element instances from.</param>
        /// <returns>IQueryable to retrieve Documents.DocumentClasses.OrderDoc instances</returns>
        public static IQueryable<Documents.DocumentClasses.OrderDoc> ProjectToOrderDoc(this IQueryable<Northwind.Adapter.EntityClasses.Order> baseQuery)
        {
            return baseQuery.Select(p__0 => new Documents.DocumentClasses.OrderDoc()
            {
                CompanyName = p__0.Customer.CompanyName,
                ContactName = p__0.Customer.ContactName,
                CustomerId = p__0.Customer.CustomerId,
                OrderDate = p__0.OrderDate,
                OrderId = p__0.OrderId,
                RequiredDate = p__0.RequiredDate,
            });
        }
    }
}

@FransBouma
Copy link
Author

//------------------------------------------------------------------------------
// <auto-generated>This code was generated by LLBLGen Pro v5.0.</auto-generated>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace Documents.DocumentClasses
{ 
    /// <summary> Document class which is derived from the entity 'Order'.</summary>
    public class OrderDoc
    {
        /// <summary>Gets or sets the CompanyName field. Derived from Entity Model Field 'Customer.CompanyName (Customer)'</summary>
        public System.String CompanyName { get; set; }
        /// <summary>Gets or sets the ContactName field. Derived from Entity Model Field 'Customer.ContactName (Customer)'</summary>
        public System.String ContactName { get; set; }
        /// <summary>Gets or sets the CustomerId field. Derived from Entity Model Field 'Customer.CustomerId (Customer)'</summary>
        public System.String CustomerId { get; set; }
        /// <summary>Gets or sets the OrderDate field. Derived from Entity Model Field 'Order.OrderDate'</summary>
        public Nullable<System.DateTime> OrderDate { get; set; }
        /// <summary>Gets or sets the OrderId field. Derived from Entity Model Field 'Order.OrderId'</summary>
        [System.ComponentModel.DataAnnotations.Key]
        public System.Int32 OrderId { get; set; }
        /// <summary>Gets or sets the RequiredDate field. Derived from Entity Model Field 'Order.RequiredDate'</summary>
        public Nullable<System.DateTime> RequiredDate { get; set; }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment