RSS

Tag Archives: Entity Framework

Repository and Unit of Work that is Fully Generic Part 3

So now for the last part the extensions to DBContext

using System;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Metadata.Edm;
using System.Linq;
using System.Reflection;

namespace MyCompany.Repo.Generic.Extensions
{
    ///
/// Code First extensions.
    ///
    public static class DbContextExtensions
    {
        ///
/// Adds an entity (if newly created) or update (if has non-default Id).
        ///
        ///
        ///The db context.
        ///The entity.
        ///
        ///
        /// Will not work for HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).
        /// Will not work for composite keys.
        ///
        public static T AddOrUpdate(this DbContext context, T entity)
            where T : class
        {
            if (context == null) throw new ArgumentNullException("context");
            if (entity == null) throw new ArgumentNullException("entity");

            if (IsTransient(context, entity))
            {
                context.Set().Add(entity);
            }
            else
            {
                context.Set().Attach(entity);
                context.Entry(entity).State = EntityState.Modified;
            }
            return entity;
        }

        ///
/// Determines whether the specified entity is newly created (Id not specified).
        ///
        ///
        ///The context.
        ///The entity.
        ///
        ///   true if the specified entity is transient; otherwise, false.
        ///
        ///
        /// Will not work for HasDatabaseGeneratedOption(DatabaseGeneratedOption.None).
        /// Will not work for composite keys.
        ///
        public static bool IsTransient(this DbContext context, T entity)
            where T : class
        {
            if (context == null) throw new ArgumentNullException("context");
            if (entity == null) throw new ArgumentNullException("entity");

            var propertyInfo = FindPrimaryKeyProperty(context);
            var propertyType = propertyInfo.PropertyType;
            //what's the default value for the type?
            var transientValue = propertyType.IsValueType ?
                Activator.CreateInstance(propertyType) : null;
            //is the pk the same as the default value (int == 0, string == null ...)
            return Equals(propertyInfo.GetValue(entity, null), transientValue);
        }

        ///
/// Loads a stub entity (or actual entity if already loaded).
        ///
        ///
        ///The context.
        ///The id.
        ///
        ///
        /// Will not work for composite keys.
        ///
        public static T Load(this DbContext context, object id)
             where T : class
        {
            if (context == null) throw new ArgumentNullException("context");
            if (id == null) throw new ArgumentNullException("id");

            var property = FindPrimaryKeyProperty(context);
            //check to see if it's already loaded (slow if large numbers loaded)
            var entity = context.Set().Local
                .FirstOrDefault(x => id.Equals(property.GetValue(x, null)));
            if (entity == null)
            {
                //it's not loaded, just create a stub with only primary key set
                entity = CreateEntity(id, property);

                context.Set().Attach(entity);
            }
            return entity;
        }

        ///
/// Determines whether the specified entity is loaded from the database.
        ///
        ///
        ///The context.
        ///The id.
        ///
        ///   true if the specified entity is loaded; otherwise, false.
        ///
        ///
        /// Will not work for composite keys.
        ///
        public static bool IsLoaded(this DbContext context, object id)
            where T : class
        {
            if (context == null) throw new ArgumentNullException("context");
            if (id == null) throw new ArgumentNullException("id");

            var property = FindPrimaryKeyProperty(context);
            //check to see if it's already loaded (slow if large numbers loaded)
            var entity = context.Set().Local
                .FirstOrDefault(x => id.Equals(property.GetValue(x, null)));
            return entity != null;
        }

        ///
/// Marks the reference navigation properties unchanged.
        /// Use when adding a new entity whose references are known to be unchanged.
        ///
        ///
        ///The context.
        ///The entity.
        public static void MarkReferencesUnchanged(DbContext context, T entity)
            where T : class
        {
            var objectContext = ((IObjectContextAdapter)context).ObjectContext;
            var objectSet = objectContext.CreateObjectSet();
            var elementType = objectSet.EntitySet.ElementType;
            var navigationProperties = elementType.NavigationProperties;
            //the references
            var references = from navigationProperty in navigationProperties
                             let end = navigationProperty.ToEndMember
                             where end.RelationshipMultiplicity == RelationshipMultiplicity.ZeroOrOne ||
                             end.RelationshipMultiplicity == RelationshipMultiplicity.One
                             select navigationProperty.Name;
            //Note: We don't check Collections. EF wants to handle the object graph so we let it.

            var parentEntityState = context.Entry(entity).State;
            foreach (var navigationProperty in references)
            {
                //if it's modified but not loaded, don't need to touch it
                if (parentEntityState == EntityState.Modified &&
                    !context.Entry(entity).Reference(navigationProperty).IsLoaded)
                    continue;
                var propertyInfo = typeof(T).GetProperty(navigationProperty);
                var value = propertyInfo.GetValue(entity, null);
                context.Entry(value).State = EntityState.Unchanged;
            }
        }

        private static PropertyInfo FindPrimaryKeyProperty(IObjectContextAdapter context)
            where T : class
        {
            //find the primary key
            var objectContext = context.ObjectContext;
            //this will error if it's not a mapped entity
            var objectSet = objectContext.CreateObjectSet();
            var elementType = objectSet.EntitySet.ElementType;
            var pk = elementType.KeyMembers.First();
            //look it up on the entity
            var propertyInfo = typeof(T).GetProperty(pk.Name);
            return propertyInfo;
        }

        private static T CreateEntity(object id, PropertyInfo property)
            where T : class
        {
            // consider IoC here
            var entity = (T)Activator.CreateInstance(typeof(T));
            //set the value of the primary key (may error if wrong type)
            property.SetValue(entity, id, null);
            return entity;
        }
    }
}

  1. Create an Entity project
  2. Add EF 5 from nugget
  3. Add an ADO.Net Entity Data Model of your database
  4. Right click on the designer and click “Add Code Generation Item”
  5. If you do not have the EF 5.x DbContect Generator installed already click online templates select the EF 5.x DbContect Generator, name it appropriately, and click add.
  6. If they do not generat off the bat open the .tt and put in the correct .edmx file name and your db context and poco classes will generate
  7. Add the connection string into the consuming app and add your entity project and the generic repo to your consuming app. Then do something like this in you BLL J
            NPM_EXTEntities context = new NPM_EXTEntities();
            IUnitOfWork efu = new UoWFactory(context).getUoW();

            IGenericRepository<Application>; 
            applications = efu.GetRepository<Application>;
 
Leave a comment

Posted by on September 4, 2013 in C#, Entity Framework, Repository, SQL, Unit Of Work

 

Tags: , , , , , ,

Repository and Unit of Work that is Fully Generic Part 1

So I have been working on creating a generic repository and unit of work and man there are no full examples out there. The following code I wrote is a combination of mine and other peoples work that I referenced combined into what I like to think is a very strong generic unit of work and repository that does not need to have the repositories declared in the unit of work :). Also on your POCO’s if you want EF to throw conncurency errors for Store Wins instead of Client Wins use the timestamp decorator from System.ComponentModel.DataAnnotations. Here is the code

IUnitOfWork

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyCompany.Repo.Generic
{
    public interface IUnitOfWork : IDisposable
    {
        IGenericRepository<T> GetRepository<T>() where T : class;
        void SaveChanges();
        void RollBack();
    }
}

IGenericRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace MyCompany.Repo.Generic
{
    public interface IGenericRepository<T> where T : class
    {
        IQueryable<T> Get { get; }
        IQueryable<T> GetIncluding(params Expression<Func<T, object>>[] includeProperties);
        T Find(object[] keyValues);
        void Add(T entity);
        void Update(T entity);
        void AddOrUpdate(T entity);
        void Delete(object[] keyValues);
    }
}

Base Generic Repository

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyCompany.Repo.Generic
{
    /// <summary>
    /// This class is used to allow you to store the repositories in a collection so they stay in scope 
    /// of the unit of work since in .net there is no class<?> like there is in Java
    /// </summary>
    public abstract class BaseGenericRepository
    {
        public abstract Type Type { get; }
    }
}

Ther rest is to come in part 2

 
1 Comment

Posted by on September 4, 2013 in C#, Entity Framework, Repository, SQL, Unit Of Work

 

Tags: , , , , , ,

Entity Framework Error 3004

Error 3004: Problem in mapping fragments starting at line <#>: No mapping specified for properties MyEntity.MyValue in Set MyEntities. An Entity with Key (PK) will not round-trip when: Entity is type [MyEntities.MyEntity]

This is a annoying error and the best thing to do is delete the offending table and regenerate the model. Not the greatest answer but when your pressed for time it is what it is.

 
Leave a comment

Posted by on January 2, 2013 in C#, Entity Framework, SQL

 

Tags: , ,

Use “LIKE()” Operation for Entity Frame Work (yes it is possible)

Alright so this is a tie in with the previous post but is still separate. I will get to that in the next post. So linq to entities was nice enough not to give us a true like operator like in linq to sql and if you use the linq to sql it throws a error and does not work against entities. So i needed a way and I found one but catch is it only works out of the box with mssql and for now we will stick with that. Sorry if you use oracle and the like suck to be there at the moment.

So here you go I have created a instance of a ObjectQuery and now I am adding on my filters for final execution and in this one I need to return a name with wild cards in place of spaces and special characters

vb .net

            Dim searchname As String = String.Format("%{0}%", replaceSpecialCharactersWith(TextBoxName.Text, "%"))
            entity = From client In entity.AsQueryable Where System.Data.Objects.SqlClient.SqlFunctions.PatIndex(searchname, client.Name) > 0 

C#

            string searchname = string.Format("%{0}%", replaceSpecialCharactersWith(TextBoxName.Text, "%"));
            entity = from client in entity.AsQueryablewhere System.Data.Objects.SqlClient.SqlFunctions.PatIndex(searchname, client.Name) > 0;

as you can see it uses the patindex function method from sql this along with deffered execution will allow you to do likes and then combine with the Or operator and you are good to go. Looking at the trace also it only tacked on a where calling the function so not much of a performance hit eithier. So using the code method below and this = automatic like on full name fields from a single textbox search :).

 
Leave a comment

Posted by on July 9, 2012 in C#, Entity Framework, SQL

 

Tags: , , ,

Replace Special And Non-ASCII Characters Without the Performance Hit of Regular Expressions

Alright so if your reading this you should know what Regular Expressions are, Now if you don’t go and use Google then come back :). Alright now that that is out of the way. So I needed to replace any special characters in a string before I passed it to another method to process it this case it was for a search function. Now here is the thing yes you can declare a reg-ex and then do a string.replace with the reg-ex but that caused a sever performance hit as it iterates through the whole expression for each character in the string. Alright now what about the people who use non-ASCII characters(yes some people do for some reason) with the code below it removes them also unlike reg-ex and by using the .net methods below you decrease the performance hit by over 2 fold.

For me this was a big deal because the data-source I was working with was huge. Now here is a basic truth you need to accept AND NOT DO WITH MY CODE before I give it to you, Strings in .net are Immutable if you don’t know what that is read the last part of the first sentence in this writeup. Alright…. if you are going to replace the special characters with quote quote DO NOT use “” as for each replacement it creates a separate string object until it leaves the for loop. Use String.Empty as the input parameter. So here you go in vb.net and C#….
VB.Net

    Public Shared Function replaceSpecialCharactersWith(text As String, replacement As String) As String
        Dim sb As New StringBuilder()
        Dim lastWasInvalid = False
        For Each c As Char In text
            If Char.IsLetterOrDigit(c) Then
                sb.Append(c)
                lastWasInvalid = False
            Else
                If Not lastWasInvalid Then
                    sb.Append(replacement)
                End If
                lastWasInvalid = True
            End If
        Next

        Return sb.ToString().ToLowerInvariant().Trim()

    End Function

C#

public static string replaceSpecialCharactersWith(string text, string replacement)
{
	StringBuilder strb = new StringBuilder();
	object lastWasInvalid = false;
	foreach (char c in text) 
	{
		if (char.IsLetterOrDigit(c)) 
		{
			strb.Append(c);
			lastWasInvalid = false;
		} 
		else 
		{
			if (!lastWasInvalid) 
			{
				strb.Append(replacement);
			}
			lastWasInvalid = true;
		}
	}

So for those who don’t understand it just in case, what it is doing is converting the string to a character array and looping through the character array and inserting the designated value for each if the if condition is met. If the character is valid it adds it to the string builder and moves to the next character. If the character is invalid it replaces it in the string builder with the specified replacement and marks the last character as invalid. The last part is so you don’t put 2 replacements side by side it just replaces once and goes till it finds another valid character :). Have fun and till next time don’t let your code have a meltdown.

 
Leave a comment

Posted by on July 9, 2012 in C#, Entity Framework

 

Tags: , , ,

Convert LINQ to Entity ObjectQuery to DataTable

It has been a while, I know and I have been meaning to blog but have been uber busy so here is a new one for you.
So for anyone who has used LINQ to DataSet you know there is a method for CopyToDataTable(). For LINQ to Entity there is no such thing and though I will not go into why there are reasons and they make partial sense but I needed to do this to get better control of my data binding. So I went out and used reflection and came back with a simple way to convert the returned IEnumerable object list to a DataTable this treats each object in the returned entity as a DataRow and dynamically gets the properties and the column names. Below are the C# and VB.Net versions. Also make sure to include the System.Reflection Library in your class where you use this.

    Public Function ObjectQueryToDataTable(Of T)(objlist As IEnumerable(Of T)) As DataTable
        Dim dtReturn As New DataTable()

        Dim objProps As PropertyInfo() = Nothing

        If objlist Is Nothing Then
            Return dtReturn
        End If

        For Each objRec As T In objlist
            If objProps Is Nothing Then
                objProps = DirectCast(objRec.[GetType](), Type).GetProperties()
                For Each objpi As PropertyInfo In objProps
                    Dim colType As Type = objpi.PropertyType

                    If (colType.IsGenericType) AndAlso (colType.GetGenericTypeDefinition() = GetType(Nullable(Of ))) Then
                        colType = colType.GetGenericArguments()(0)
                    End If

                    dtReturn.Columns.Add(New DataColumn(objpi.Name, colType))
                Next
            End If

            Dim dr As DataRow = dtReturn.NewRow()

            For Each pi As PropertyInfo In objProps
                dr(pi.Name) = If(pi.GetValue(objRec, Nothing) Is Nothing, DBNull.Value, pi.GetValue(objRec, Nothing))
            Next

            dtReturn.Rows.Add(dr)
        Next
        Return dtReturn
    End Function
public DataTable ObjectQueryToDataTable<T>(IEnumerable<T> objlist)
{
	DataTable dtReturn = new DataTable();

	PropertyInfo[] objProps = null;

	if (objlist == null) {
		return dtReturn;
	}

	foreach (T objRec in objlist) {
		if (objProps == null) {
			objProps = ((Type)objRec.GetType()).GetProperties();
			foreach (PropertyInfo objpi in objProps) {
				Type colType = objpi.PropertyType;

				if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>))) {
					colType = colType.GetGenericArguments()(0);
				}

				dtReturn.Columns.Add(new DataColumn(objpi.Name, colType));
			}
		}

		DataRow dr = dtReturn.NewRow();

		foreach (PropertyInfo pi in objProps) {
			dr(pi.Name) = pi.GetValue(objRec, null) == null ? DBNull.Value : pi.GetValue(objRec, null);
		}

		dtReturn.Rows.Add(dr);
	}
	return dtReturn;
}

Hope this helps you out as always if you have a better way let me know :).

 
2 Comments

Posted by on July 6, 2012 in ASP.Net, C#, Entity Framework, SQL

 

Tags: , , ,