This is a negative case but I think it is relatively easy to hit when fiddling with the model in model conventions.
Adding an element to the storage model with a namespace name different from the one other elements in the model are using will cause an exception with message "Sequence contains no elements" during initialization from the following line of XDocumentExtensions.GetStorageMappingItemCollection:
```
var ssdlSchemaElement = model.Descendants(EdmXNames.Ssdl.SchemaNames).Single();
```
For example, the following store model convention adds a function with a namespace that is different from the default namespace used by code first:
```
public class StoreFunctionBomb : IStoreModelConvention<EdmModel>
{
public void Apply(EdmModel item, DbModel model)
{
var payload = new EdmFunctionPayload()
{
StoreFunctionName = "CrashSerializer",
Parameters = new FunctionParameter[] {},
ReturnParameters = new FunctionParameter[] {},
Schema = "dbo"
};
var incorrectNamespaceName = "Boom";
var function = EdmFunction.Create("CrashSerializer",
incorrectNamespaceName,
DataSpace.SSpace,
payload,
null);
item.AddItem(function);
}
}
```
#Cause
Currently our SSDL and CSDL serializers are limited to serializing model items in only one namespace (this all the EF Designer and Code First require). The following code in SsdlSerializer will declare the artifact invalid if there is more than one distinct namespace name used in the model, causing the SsdlSerializer.Serialize method to not serialize and return false:
```
if (model.NamespaceNames.Count() > 1
|| model.Containers.Count() != 1)
{
onErrorAction(
new DataModelErrorEventArgs
{
ErrorMessage = Strings.Serializer_OneNamespaceAndOneContainer,
});
}
```
When the EDMX serializer invokes the SsdlSerializer, it will silently ignore the result of the Serialize method, causing it to output an EDMX that does not contain the SSDL:
```
using (Element("StorageModels"))
{
new SsdlSerializer().Serialize(
_databaseMapping.Database,
_databaseMapping.ProviderInfo.ProviderInvariantName,
_databaseMapping.ProviderInfo.ProviderManifestToken,
_xmlWriter);
}
```
When EDMX serialization happens as part of model diffing such an EDMX missing the SSDL part will cause the call to Enumerable.Single in the aforementioned XDocumentExtensions.GetStorageMappingItemCollection method to fail.
Note that I have not tried to repro this on CSDL, but it is possible that something similar happens as there is a similar restriction.
Adding an element to the storage model with a namespace name different from the one other elements in the model are using will cause an exception with message "Sequence contains no elements" during initialization from the following line of XDocumentExtensions.GetStorageMappingItemCollection:
```
var ssdlSchemaElement = model.Descendants(EdmXNames.Ssdl.SchemaNames).Single();
```
For example, the following store model convention adds a function with a namespace that is different from the default namespace used by code first:
```
public class StoreFunctionBomb : IStoreModelConvention<EdmModel>
{
public void Apply(EdmModel item, DbModel model)
{
var payload = new EdmFunctionPayload()
{
StoreFunctionName = "CrashSerializer",
Parameters = new FunctionParameter[] {},
ReturnParameters = new FunctionParameter[] {},
Schema = "dbo"
};
var incorrectNamespaceName = "Boom";
var function = EdmFunction.Create("CrashSerializer",
incorrectNamespaceName,
DataSpace.SSpace,
payload,
null);
item.AddItem(function);
}
}
```
#Cause
Currently our SSDL and CSDL serializers are limited to serializing model items in only one namespace (this all the EF Designer and Code First require). The following code in SsdlSerializer will declare the artifact invalid if there is more than one distinct namespace name used in the model, causing the SsdlSerializer.Serialize method to not serialize and return false:
```
if (model.NamespaceNames.Count() > 1
|| model.Containers.Count() != 1)
{
onErrorAction(
new DataModelErrorEventArgs
{
ErrorMessage = Strings.Serializer_OneNamespaceAndOneContainer,
});
}
```
When the EDMX serializer invokes the SsdlSerializer, it will silently ignore the result of the Serialize method, causing it to output an EDMX that does not contain the SSDL:
```
using (Element("StorageModels"))
{
new SsdlSerializer().Serialize(
_databaseMapping.Database,
_databaseMapping.ProviderInfo.ProviderInvariantName,
_databaseMapping.ProviderInfo.ProviderManifestToken,
_xmlWriter);
}
```
When EDMX serialization happens as part of model diffing such an EDMX missing the SSDL part will cause the call to Enumerable.Single in the aforementioned XDocumentExtensions.GetStorageMappingItemCollection method to fail.
Note that I have not tried to repro this on CSDL, but it is possible that something similar happens as there is a similar restriction.