c# - Entity Framework 4.0 - Get By ID Using Generics and Reflection -


I generic and property will be able to load a unit ID using reflection, but this work is that it is uncertain How to Complete Entity Framework 4.0

I have a method in my abstract pattern such as:

  public abstract T GetById (object ID, Test Contact reference);  

Currently, since it is the essence, I have made this method that I every made in a repository class:

  public override Testobset GetById (object) ID, Test Contact reference) {return context. TestObject.First (x = & gt; x.TestId == (int) ID); }  

Is this essence a way to use reflection and generic in my abstract class?

This is a VB example I use. My keys are all GUIDs, but you can probably customize this.

  public shared function GetKeyPropertyName (ByVal TypeName As String) As String Dim props = Type.GetType (TypeName) dim each Prop props Atts = prop.GetCustomAttributes ( True) for each att .getProperties Atts is typeof (att) EdmScalarPropertyAttribute so then DirectCast (att, EdmScalarPropertyAttribute) .EntityKeyProperty then return prop.Name end if end then the next next new ApplicationException (String.Format ("Any key property type Found to throw '{0}'. ", TypeName) Function GetObjectById (as EntityObject t) (ByVal id as as Guid) T Dim ctx = MyContext dim entityType Type = GetType (T) as dull entityTypeName string = entityType.ToString dim keyProperty String = GetKeyPropertyName (entityTypeName) Dim Container = ctx.MetadataWorkspace.GetEntityContainer (ctx.DefaultContainerName, Metadata.Edm.DataSpace.CS Speed) in the form of slow entitySetName = string (Meta.State.BaseEntitySets from Meta.ElementType.FullName = EntityTypeName meta.Name) First () retarded entitySetFullName string = "String.Format" ("{0}. {1} ", container.Name, entitySetName) Dim entityKeyValues ​​IEnumerable (Key, Object, of KeyValuePair) = _ New KeyValuePair (string, object) () as {New KeyValuePair (string, object) (keyProperty , Id) return to the newest entity (entity, full name, entity key values) in the form of a slow key (ctx.GetObjectByKey (key), T) and function  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -