1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace Microsoft.DbContextPackage.Utilities
{
using System;
using System.Collections.Generic;
using System.Data.Mapping;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.DbContextPackage.Extensions;
using Microsoft.DbContextPackage.Resources;
using Xunit;
public class EdmxUtilityTests
{
[Fact]
public void GetMappingCollection_returns_mapping()
{
var edmxContents = @"<?xml version='1.0'?>
<Edmx Version='3.0' xmlns='http://schemas.microsoft.com/ado/2009/11/edmx'>
<Runtime>
<ConceptualModels>
<Schema Namespace='Model' Alias='Self' annotation:UseStrongSpatialTypes='false' xmlns:annotation='http://schemas.microsoft.com/ado/2009/02/edm/annotation' xmlns='http://schemas.microsoft.com/ado/2009/11/edm'>
<EntityContainer Name='DatabaseEntities' annotation:LazyLoadingEnabled='true'>
<EntitySet Name='Entities' EntityType='Model.Entity' />
</EntityContainer>
<EntityType Name='Entity'>
<Key>
<PropertyRef Name='Id' />
</Key>
<Property Name='Id' Type='Int32' Nullable='false' annotation:StoreGeneratedPattern='Identity' />
</EntityType>
</Schema>
</ConceptualModels>
<Mappings>
<Mapping Space='C-S' xmlns='http://schemas.microsoft.com/ado/2009/11/mapping/cs'>
<EntityContainerMapping StorageEntityContainer='ModelStoreContainer' CdmEntityContainer='DatabaseEntities'>
<EntitySetMapping Name='Entities'>
<EntityTypeMapping TypeName='Model.Entity'>
<MappingFragment StoreEntitySet='Entities'>
<ScalarProperty Name='Id' ColumnName='Id' />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</Mappings>
<StorageModels>
<Schema Namespace='Model.Store' Alias='Self' Provider='System.Data.SqlClient' ProviderManifestToken='2008' xmlns:store='http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator' xmlns='http://schemas.microsoft.com/ado/2009/11/edm/ssdl'>
<EntityContainer Name='ModelStoreContainer'>
<EntitySet Name='Entities' EntityType='Model.Store.Entities' store:Type='Tables' Schema='dbo' />
</EntityContainer>
<EntityType Name='Entities'>
<Key>
<PropertyRef Name='Id' />
</Key>
<Property Name='Id' Type='int' Nullable='false' StoreGeneratedPattern='Identity' />
</EntityType>
</Schema>
</StorageModels>
</Runtime>
</Edmx>";
StorageMappingItemCollection mappingCollection;
using (var edmx = new TempFile(edmxContents))
{
mappingCollection = new EdmxUtility(edmx.FileName)
.GetMappingCollection();
}
Assert.True(mappingCollection.Contains("DatabaseEntities"));
}
[Fact]
public void GetMappingCollection_returns_mapping_for_v2_schema()
{
var edmxContents = @"<?xml version='1.0' ?>
<Edmx Version='2.0' xmlns='http://schemas.microsoft.com/ado/2008/10/edmx'>
<Runtime>
<ConceptualModels>
<Schema Namespace='Model' Alias='Self' xmlns:annotation='http://schemas.microsoft.com/ado/2009/02/edm/annotation' xmlns='http://schemas.microsoft.com/ado/2008/09/edm'>
<EntityContainer Name='DatabaseEntities' annotation:LazyLoadingEnabled='true'>
<EntitySet Name='Entities' EntityType='Model.Entity' />
</EntityContainer>
<EntityType Name='Entity'>
<Key>
<PropertyRef Name='Id' />
</Key>
<Property Name='Id' Type='Int32' Nullable='false' annotation:StoreGeneratedPattern='Identity' />
</EntityType>
</Schema>
</ConceptualModels>
<Mappings>
<Mapping Space='C-S' xmlns='http://schemas.microsoft.com/ado/2008/09/mapping/cs'>
<EntityContainerMapping StorageEntityContainer='ModelStoreContainer' CdmEntityContainer='DatabaseEntities'>
<EntitySetMapping Name='Entities'>
<EntityTypeMapping TypeName='Model.Entity'>
<MappingFragment StoreEntitySet='Entities'>
<ScalarProperty Name='Id' ColumnName='Id' />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</Mappings>
<StorageModels>
<Schema Namespace='Model.Store' Alias='Self' Provider='System.Data.SqlClient' ProviderManifestToken='2008' xmlns:store='http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator' xmlns='http://schemas.microsoft.com/ado/2009/02/edm/ssdl'>
<EntityContainer Name='ModelStoreContainer'>
<EntitySet Name='Entities' EntityType='Model.Store.Entities' store:Type='Tables' Schema='dbo' />
</EntityContainer>
<EntityType Name='Entities'>
<Key>
<PropertyRef Name='Id' />
</Key>
<Property Name='Id' Type='int' Nullable='false' StoreGeneratedPattern='Identity' />
</EntityType>
</Schema>
</StorageModels>
</Runtime>
</Edmx>";
StorageMappingItemCollection mappingCollection;
using (var edmx = new TempFile(edmxContents))
{
mappingCollection = new EdmxUtility(edmx.FileName)
.GetMappingCollection();
}
Assert.True(mappingCollection.Contains("DatabaseEntities"));
}
[Fact]
public void GetMappingCollection_throws_on_schema_errors()
{
var edmxContents = @"<?xml version='1.0'?>
<Edmx Version='3.0' xmlns='http://schemas.microsoft.com/ado/2009/11/edmx'>
<Runtime>
<ConceptualModels>
<Schema xmlns='http://schemas.microsoft.com/ado/2009/11/edm' />
</ConceptualModels>
</Runtime>
</Edmx>";
using (var edmx = new TempFile(edmxContents))
{
var ex = Assert.Throws<EdmSchemaErrorException>(
() => new EdmxUtility(edmx.FileName).GetMappingCollection());
Assert.Equal(
Strings.EdmSchemaError(
Path.GetFileName(edmx.FileName),
"ConceptualModels"),
ex.Message);
}
}
private sealed class TempFile : IDisposable
{
private readonly string _fileName;
private bool _disposed;
public TempFile(string contents)
{
_fileName = Path.GetTempFileName();
File.WriteAllText(_fileName, contents);
}
~TempFile()
{
Dispose(false);
}
public string FileName
{
get
{
HandleDisposed();
return _fileName;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (!string.IsNullOrWhiteSpace(_fileName))
{
File.Delete(_fileName);
}
_disposed = true;
}
}
private void HandleDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(null);
}
}
}
}
}
|