![]() This code is contained in the file in the EntitiesLibrary project:ġ9: fileManager.StartNewFile(entity.Name + ".cs") Ģ5: using ģ3: //We need to hook this up in order to make the buddy class mechanism work in WPF, Console Applications etc.ģ4: //Or the Validator methods won't make use of the metadata class.ģ6: Type currentType = MethodBase.GetCurrentMethod().DeclaringType ģ7: object attributes = currentType.GetCustomAttributes( typeof(MetadataTypeAttribute), false) Ĥ1: MetadataTypeAttribute metaDataAttribute = (MetadataTypeAttribute)attributes Ĥ2: TypeDescriptor.AddProviderTransparent(Ĥ3: new AssociatedMetadataTypeTypeDescriptionProvider(Ĥ4: currentType, metaDataAttribute.MetadataClassType),currentType) ĥ3: ValidationContext vc = new ValidationContext( this, null, null) ĥ5: if (!Validator.TryValidateObject( this, vc, result))Ħ6: public string thisħ0: ValidationContext vc = new ValidationContext( this, null, null) ħ3: if (!Validator.TryValidateProperty(GetType().GetProperty(columnName).GetValue( this, null), vc, result))Ĩ4: p.TypeUsage.EdmType is PrimitiveType & p.DeclaringType = entity & p.DefaultValue != null) Ĩ6: var collectionNavigationProperties = (np => np.DeclaringType = entity & np.ToEndMember.RelationshipMultiplicity = RelationshipMultiplicity.Many) Ĩ7: var complexProperties = (p => p.TypeUsage.EdmType is ComplexType & p.DeclaringType = entity) Ĩ9: if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())ġ18: p.TypeUsage.EdmType is PrimitiveType & p.DeclaringType = entity) ġ24: foreach (var edmProperty in primitiveProperties)ġ60: fileManager.StartNewFile(complex.Name + ".cs") ġ61: BeginNamespace(namespaceName, code) ġ67: p.TypeUsage.EdmType is ComplexType & p.DeclaringType = complex) ġ69: var propertiesWithDefaultValues = (p => p.TypeUsage.EdmType is PrimitiveType & p.DeclaringType = complex & p.DefaultValue != null) ġ71: if (propertiesWithDefaultValues.Any() || complexProperties.Any())ġ93: p.TypeUsage.EdmType is PrimitiveType & p.DeclaringType = complex) ġ99: foreach(var edmProperty in primitiveProperties)Ģ45: //-Ģ52: //-Ģ98: code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),Ģ99: code.SpaceAfter(Accessibility.ForSetter(navigationProperty))) ģ02: void WriteProperty( string accessibility, string type, string name, string getterAccessibility, string setterAccessibility)ģ17: foreach(var type in itemCollection.GetItems())ģ19: if (!(type is EntityType || type is ComplexType))ģ24: if (alreadySeen.ContainsKey(type.FullName))ģ26: Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types are not supported", type.FullName)) ģ31: alreadySeen.Add(type.FullName, true) I’ve added some validation to this class using the buddy class mechanism. In this article I will only have one entity, namely Product. ![]() If you do this, you’ll have to add a namespace in the T4 template that generates the DbContext, so that DbContext knows the entity classes. I’ve simply moved the one that generates the entities to it’s own class library. The one with “.Context” in it generates your DbContext class, the other one generates POCO entities. ![]() When you use this template you’ll actually get two T4 templates added to your project. I started with the DBContextLibrary and used the new “DbContext Generator” T4 template to generate classes. Let’s first start with the two projects that are responsible for my data access classes, including the entities:īoth DbContextLibrary and EntitiesLibrary are normal class library projects. That’s where the Entity Framework 4.1 part is explained. If you don’t like WPF or you just don’t care, feel free the skip to the “WCF Service” paragraph. In this article I’ll show you how to decorate your classes with validation attributes and how these classes can be reused by both the client and the server. The DbContext has a feature which simply isn’t supported by the ObjectContext: data validation with the attributes from the namespace. ![]() Now you might think as I did: “But if I’m comfortable with the ObjectContext class and I don’t use code first, why in the world would I need a DbContext?”. As you’ll probably know, the new DBContext class is largely a wrapper around the ObjectContext class with a simplified API. What did catch my eye is the new DBContext api. This is because in my line of business we deal with stored procedures a lot and it turns out that mapping to stored procedures isn’t supported with code first in the 4.1 release. While most people were very enthusiastic about the code first stuff, code first was the part that impressed me the least. Recently Entity Framework 4.1 was released.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |