GetAutoGeneratedKeyType() public method

Get the autoGeneratedKeyType value for the given type. Should be defined even if the actual key property is on a base type.
public GetAutoGeneratedKeyType ( Type type ) : string
type System.Type Entity type for which metadata is being generated
return string
コード例 #1
0
        /// <summary>
        /// Add the metadata for an entity.
        /// </summary>
        /// <param name="type">Type for which metadata is being generated</param>
        void AddType(Type type)
        {
            // "Customer:#Breeze.Models.NorthwindIBModel": {
            var classKey = type.Name + ":#" + type.Namespace;
            var cmap     = new Dictionary <string, object>();

            _typeList.Add(cmap);
            _typeMap.Add(type, cmap);

            cmap.Add("shortName", type.Name);
            cmap.Add("namespace", type.Namespace);
            if (!type.IsInterface)
            {
                var interfaces = type.GetInterfaces().Except(type.BaseType.GetInterfaces()).Where(t => _types.Contains(t)).Select(t => t.Name).ToList();
                if (interfaces.Any())
                {
                    var custom = new Dictionary <string, object>()
                    {
                        { "interfaces", string.Join(", ", interfaces) }
                    };
                    cmap.Add("custom", custom);
                }
            }

            if (_describer.IsComplexType(type))
            {
                cmap.Add("isComplexType", true);
            }
            else
            {
                // Only identify the base type if it is also an entity in the type list
                if (_entityTypes.Contains(type.BaseType))
                {
                    var baseTypeName = type.BaseType.Name + ":#" + type.BaseType.Namespace;
                    cmap.Add("baseTypeName", baseTypeName);
                }

                if (type.IsAbstract)
                {
                    cmap.Add("isAbstract", true);
                }
                // Get the autoGeneratedKeyType for this type
                var keyGenerator = _describer.GetAutoGeneratedKeyType(type);
                if (keyGenerator != null)
                {
                    cmap.Add("autoGeneratedKeyType", keyGenerator);
                }

                var resourceName = _describer.GetResourceName(type);
                cmap.Add("defaultResourceName", resourceName);
                _resourceMap.Add(resourceName, classKey);
            }


            var dataList = new List <Dictionary <string, object> >();

            cmap.Add("dataProperties", dataList);

            AddDataProperties(type, dataList);

            // Change the autoGeneratedKeyType if an attribute was found on a data property
            var keyProp = FindEntry(dataList, "isPartOfKey");

            if (keyProp != null)
            {
                var custom = keyProp.Get("custom");
                if ("Identity".Equals(custom))
                {
                    cmap["autoGeneratedKeyType"] = "Identity";
                    keyProp.Remove("custom");
                }
                else if ("Computed".Equals(custom))
                {
                    cmap["autoGeneratedKeyType"] = "KeyGenerator";
                    keyProp.Remove("custom");
                }
            }
            else if (!type.IsAbstract && !type.IsEnum && !_describer.IsComplexType(type) && !_entityTypes.Contains(type.BaseType))
            {
                // No key for an entity => error or add the key
                var missingFKHandling = _describer.GetMissingPKHandling(type);
                if (missingFKHandling == MissingKeyHandling.Error)
                {
                    throw new Exception("Key not found for entity " + classKey);
                }
                else if (missingFKHandling == MissingKeyHandling.Add)
                {
                    var dmap = new Dictionary <string, object>();
                    dmap.Add("nameOnServer", type.Name + "GenKey");
                    dmap.Add("dataType", "Guid"); // TODO make this configurable
                    dmap.Add("isPartOfKey", true);
                    dmap.Add("custom", "pk_generated");
                    dataList.Add(dmap);
                }
                else
                {
                    Console.Error.WriteLine("Key not found for entity " + classKey);
                }
            }

            // Validators
            var validators = new List <Dictionary <string, object> >();
            var attributes = type.GetCustomAttributes();

            foreach (var attr in attributes)
            {
                if (attr is ValidationAttribute)
                {
                    var validator = _describer.MapValidationAttribute((ValidationAttribute)attr);
                    if (validator != null)
                    {
                        validators.Add(validator);
                    }
                }
            }

            if (validators.Any())
            {
                cmap.Add("validators", validators);
            }
        }