This is a negative case but I think it is relatively easy to hit when fiddling with the metadata in store model conventions. Note that I have not tried yet to repro this on CSDL, but it is possible that something similar happens as similar restrictions exists there.
#Symptoms
Adding an element to the storage model with a namespace name different from the one other elements in the model are using causes an InvalidOperationException with the message "Sequence contains no elements" during initialization. The exception comes from the following line in XDocumentExtensions.GetStorageMappingItemCollection:
```
var ssdlSchemaElement = model.Descendants(EdmXNames.Ssdl.SchemaNames).Single();
```
#Repro
For example, the following store model convention adds a function with a namespace that is different from the default namespace used by code first:
```
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace WhereIsMyStoreModel
{
class Program
{
static void Main(string[] args)
{
using (var db = new TownContext())
{
db.Database.Initialize(force: false);
}
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class TownContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new StoreModelBomb());
}
}
public class StoreModelBomb : 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 'false' result from 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 to check that the database and the model are compatible such an EDMX missing the SSDL part will cause the call to Enumerable.Single in the aforementioned XDocumentExtensions.GetStorageMappingItemCollection method to fail.
#Symptoms
Adding an element to the storage model with a namespace name different from the one other elements in the model are using causes an InvalidOperationException with the message "Sequence contains no elements" during initialization. The exception comes from the following line in XDocumentExtensions.GetStorageMappingItemCollection:
```
var ssdlSchemaElement = model.Descendants(EdmXNames.Ssdl.SchemaNames).Single();
```
#Repro
For example, the following store model convention adds a function with a namespace that is different from the default namespace used by code first:
```
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace WhereIsMyStoreModel
{
class Program
{
static void Main(string[] args)
{
using (var db = new TownContext())
{
db.Database.Initialize(force: false);
}
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class TownContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new StoreModelBomb());
}
}
public class StoreModelBomb : 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 'false' result from 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 to check that the database and the model are compatible such an EDMX missing the SSDL part will cause the call to Enumerable.Single in the aforementioned XDocumentExtensions.GetStorageMappingItemCollection method to fail.