• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTkx

Related Code
Generic Entity Framework 4.0 Base Repository
Generic Entity Framework 4.0 Base Repository with Paging
Keep Entity Framework ObjectContext in a WCF Service OperationContext
WCF RIA Services Unity 2.0 DomainService Factory
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Loading Strategy for Entity Framework 4.0
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
Generic linq-to-sql repository
UnitOfWork Action filter for ASP.NET MVC and nHibernate
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Custom C# exception class
Email validation with Regular Expression
Reflection dynamically get private fields or properties
Url Validation with Regular Expression
Method to hash passwords
Simple light ObjectFactory using Unity
Number of sealed / unsealed types in the framework
Extension to the HttpClient

A Default Entity Framework 4.0 ObjectContext Factory

This is a default EF ObjectContext factory, perfect when using POCOs

0
1.34k 0 0 0 0 0

public interface IObjectContextFactory
{
    ObjectContext Create();

    ObjectContext Create(string connectionString);
}


public class DefaultObjectContextFactory : IObjectContextFactory
{

    public ObjectContext Create()
    {
        return this.Create(ConfigurationManager.ConnectionStrings["AspenConnectionString"].ConnectionString);
    }


    public ObjectContext Create(string connectionString)
    {
        if (string.IsNullOrEmpty(connectionString))
            throw new ArgumentNullException("connectionString");

        var connection = new SqlConnection(connectionString);

        var workspace = new MetadataWorkspace(
                                new string[] { "res://*/" },
                                new Assembly[] { GetModelMappingConfigurationAssembly() });

        CheckAndBeSureMetadataIsLoaded(workspace);

        return this.Create(new EntityConnection(workspace, connection));
    }



    private ObjectContext Create(EntityConnection con)
    {
        if (con == null)
            throw new ArgumentNullException("con");

        return new ObjectContext(con);
    }


    private static void CheckAndBeSureMetadataIsLoaded(MetadataWorkspace workspace)
    {
        Debug.Assert(workspace.GetItemCollection(DataSpace.CSpace).GetItems<EntityType>().Count != 0);
        Debug.Assert(workspace.GetItemCollection(DataSpace.SSpace).GetItems<EntityType>().Count != 0);
    }


    private Assembly GetModelMappingConfigurationAssembly()
    {
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();

        var assembly = assemblies.Where(a => a.FullName.Contains("EntityMapping")).SingleOrDefault();

        if (assembly == null)
            throw new MappingException("Can't find the Assembly with the Entity Configuration Mapping");

        return assembly;
    }
}


Note: The GetModelMappingConfiguration will look through all referenced assemblies for a separate assembly where the EDMX file is located. Can probably be done much better, any suggestions?


Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

ef4.0

Mark 'ef4.0' tag as 'like'

Mark 'ef4.0' tag as 'ignore'

factory

Mark 'factory' tag as 'like'

Mark 'factory' tag as 'ignore'

objectcontext

Mark 'objectcontext' tag as 'like'

Mark 'objectcontext' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 14, 2010 7:41 AM
edited July 14, 2010 7:43 AM

Fork

 A Default Entity Framework 4.0 ObjectContext Factory -  @fredrikn Wednesday 14, 2010 7:41 AM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010