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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace FunctionalTests
{
using System;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Edm;
using System.Data.Entity.Resources;
using System.Linq;
using System.Linq.Expressions;
using FunctionalTests.Fixtures;
using FunctionalTests.Model;
using Xunit;
public class AdvancedMappingScenarioTests : TestBase
{
[Fact]
public void Sql_ce_should_get_explicit_max_lengths_for_string_and_binary_properties_by_convention()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<MaxLengthProperties>();
var databaseMapping = BuildCeMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(4000, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual("nvarchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(4000, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual("nvarchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(4000, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual("varbinary", c => c.TypeName);
}
[Fact]
public void Sql_ce_should_get_explicit_max_lengths_for_fixed_length_string_and_fixed_length_binary_properties_by_convention()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<MaxLengthProperties>().Property(e => e.Id).IsFixedLength();
modelBuilder.Entity<MaxLengthProperties>().Property(e => e.Prop1).IsFixedLength();
modelBuilder.Entity<MaxLengthProperties>().Property(e => e.Prop2).IsFixedLength();
var databaseMapping = BuildCeMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(4000, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual("nchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(4000, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual("nchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(4000, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual("binary", c => c.TypeName);
}
[Fact]
public void Sql_should_get_implicit_max_lengths_for_string_and_binary_properties_by_convention()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<MaxLengthProperties>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(128, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual("nvarchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(null, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual("nvarchar(max)", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(null, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual("varbinary(max)", c => c.TypeName);
}
[Fact]
public void Sql_should_get_explicit_max_lengths_for_fixed_length_string_and_fixed_length_binary_properties_by_convention()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<MaxLengthProperties>().Property(e => e.Id).IsFixedLength();
modelBuilder.Entity<MaxLengthProperties>().Property(e => e.Prop1).IsFixedLength();
modelBuilder.Entity<MaxLengthProperties>().Property(e => e.Prop2).IsFixedLength();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(128, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Id).DbEqual("nchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(128, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop1).DbEqual("nchar", c => c.TypeName);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(128, f => f.MaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual(false, f => f.IsMaxLength);
databaseMapping.Assert<MaxLengthProperties>(e => e.Prop2).DbEqual("binary", c => c.TypeName);
}
[Fact]
public void Can_have_configured_duplicate_column_and_by_convention_column_is_uniquified()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithConfiguredDuplicateColumn>();
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<EntityWithConfiguredDuplicateColumn>(e => e.Description).DbEqual(
"Description1",
c => c.Name);
databaseMapping.Assert<EntityWithConfiguredDuplicateColumn>(e => e.Details).DbEqual(
"Description",
c => c.Name);
}
[Fact]
public void Can_have_configured_duplicate_column_and_by_convention_columns_are_uniquified_first()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithDescBase>();
modelBuilder.Entity<EntityWithDescA>().Property(e => e.Description).HasColumnName("Description");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<EntityWithDescA>(e => e.Description).DbEqual("Description", c => c.Name);
databaseMapping.Assert<EntityWithDescB>(e => e.Description).DbEqual("Description1", c => c.Name);
databaseMapping.Assert<EntityWithDescC>(e => e.Description).DbEqual("Description2", c => c.Name);
}
[Fact]
public void Can_have_configured_duplicate_column_and_by_convention_columns_are_uniquified_second()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithDescBase>();
modelBuilder.Entity<EntityWithDescB>().Property(e => e.Description).HasColumnName("Description");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<EntityWithDescA>(e => e.Description).DbEqual("Description1", c => c.Name);
databaseMapping.Assert<EntityWithDescB>(e => e.Description).DbEqual("Description", c => c.Name);
databaseMapping.Assert<EntityWithDescC>(e => e.Description).DbEqual("Description2", c => c.Name);
}
[Fact]
public void Can_have_configured_duplicate_column_and_by_convention_columns_are_uniquified_third()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithDescBase>();
modelBuilder.Entity<EntityWithDescC>().Property(e => e.Description).HasColumnName("Description");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<EntityWithDescA>(e => e.Description).DbEqual("Description1", c => c.Name);
databaseMapping.Assert<EntityWithDescB>(e => e.Description).DbEqual("Description2", c => c.Name);
databaseMapping.Assert<EntityWithDescC>(e => e.Description).DbEqual("Description", c => c.Name);
}
[Fact]
public void Can_have_configured_duplicate_column_and_by_convention_columns_are_uniquified_complex()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithDescA>().Property(e => e.Complex.Description).HasColumnName("Description");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<ComplexWithDesc>(c => c.Description).DbEqual("Description", c => c.Name);
databaseMapping.Assert<ComplexWithDesc>(c => c.Description).DbEqual(false, c => c.Nullable);
databaseMapping.Assert<EntityWithDescA>(e => e.Description).DbEqual("Description1", c => c.Name);
}
[Fact]
public void Can_have_configured_complex_column_override_column_name_clash()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithDescA>().Property(e => e.Complex.Description).HasColumnName("Description");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<ComplexWithDesc>(c => c.Description).DbEqual(false, c => c.Nullable);
databaseMapping.Assert<ComplexWithDesc>(c => c.Description).DbEqual("Description", c => c.Name);
databaseMapping.Assert<EntityWithDescA>(e => e.Description).DbEqual("Description1", c => c.Name);
}
[Fact]
public void Can_have_configured_duplicate_column_and_by_convention_columns_are_uniquified_conflict()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<EntityWithDescBase>();
modelBuilder.Entity<EntityWithDescB>().Property(e => e.Description).HasColumnName("Description");
modelBuilder.Entity<EntityWithDescC>().Property(e => e.Description).HasColumnName("Description");
Assert.Throws<ModelValidationException>(() => BuildMapping(modelBuilder));
}
[Fact]
public void Can_table_split_and_conflicting_columns_are_uniquified()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<SplitProduct>().ToTable("Product");
modelBuilder.Entity<SplitProductDetail>().ToTable("Product");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
}
[Fact]
public void Can_table_split_and_conflicting_columns_can_be_configured()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<SplitProduct>().ToTable("Product");
modelBuilder.Entity<SplitProductDetail>().ToTable("Product");
modelBuilder.Entity<SplitProductDetail>().Property(s => s.Name).HasColumnName("Unique");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<SplitProduct>(s => s.Name).DbEqual("Name", c => c.Name);
databaseMapping.Assert<SplitProductDetail>(s => s.Name).DbEqual("Unique", c => c.Name);
}
[Fact]
public void Single_abstract_type_with_associations_throws_not_mappable_exception()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<SingleAbstract>();
Assert.Equal(
Strings.UnmappedAbstractType(typeof(SingleAbstract)),
Assert.Throws<InvalidOperationException>(
() => BuildMapping(modelBuilder)).Message);
}
[Fact]
public void Configured_decimal_key_gets_correct_facet_defaults()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<DecimalKey>().HasKey(d => d.Id);
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
databaseMapping.Assert<DecimalKey>(d => d.Id).FacetEqual((byte)18, f => f.Precision);
databaseMapping.Assert<DecimalKey>(d => d.Id).FacetEqual((byte)2, f => f.Scale);
}
[Fact]
public void Decimal_key_with_custom_store_type_should_propagate_facets()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<DecimalKey>().Property(p => p.Id).HasColumnType("money");
var databaseMapping = BuildMapping(modelBuilder);
databaseMapping.AssertValid();
}
[Fact]
public void Throw_when_mapping_properties_expression_contains_assignments()
{
var modelBuilder = new DbModelBuilder();
Expression<Func<StockOrder, object>> propertiesExpression = so => new
{
Foo = so.LocationId
};
Assert.Equal(
Strings.InvalidComplexPropertiesExpression(propertiesExpression),
Assert.Throws<InvalidOperationException>(
() =>
modelBuilder
.Entity<StockOrder>()
.Map(emc => emc.Properties(propertiesExpression)))
.Message);
}
[Fact]
public void Circular_delete_cascade_path_can_be_generated()
{
var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<StockOrder>();
var databaseMapping = BuildMapping(modelBuilder);
Assert.Equal(
3,
databaseMapping.Model
.AssociationTypes
.SelectMany(a => a.Members)
.Cast<AssociationEndMember>()
.Count(e => e.DeleteBehavior == OperationAction.Cascade));
}
[Fact]
public void Build_model_for_entity_splitting_difference_schemas()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<Vendor>()
.Map(
m =>
{
m.Properties(
v1 => new
{
v1.VendorID,
v1.Name,
v1.PreferredVendorStatus,
v1.AccountNumber,
v1.ActiveFlag,
v1.CreditRating
});
m.ToTable("Vendor", "vendors");
})
.Map(
m =>
{
m.Properties(
v2 => new
{
v2.VendorID,
v2.ModifiedDate,
v2.PurchasingWebServiceURL
});
m.ToTable("VendorDetails", "details");
});
var databaseMapping = BuildMapping(modelBuilder);
Assert.True(databaseMapping.Database.GetEntitySets().Any(s => s.Schema == "vendors"));
Assert.True(databaseMapping.Database.GetEntitySets().Any(s => s.Schema == "details"));
}
[Fact]
public void Build_model_for_mapping_to_duplicate_tables_different_schemas()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<Customer>().ToTable("tbl");
modelBuilder.Entity<Product>().ToTable("tbl", "other");
var databaseMapping = BuildMapping(modelBuilder);
Assert.True(databaseMapping.Database.GetEntitySets().Any(s => s.Schema == "dbo"));
Assert.True(databaseMapping.Database.GetEntitySets().Any(s => s.Schema == "other"));
databaseMapping.Assert<Customer>().DbEqual("tbl", t => t.Table);
databaseMapping.Assert<Product>().DbEqual("tbl", t => t.Table);
}
[Fact]
public void Build_model_after_configuring_entity_set_name()
{
var modelBuilder = new AdventureWorksModelBuilder();
modelBuilder.Entity<TransactionHistoryArchive>().HasEntitySetName("Foos");
var databaseMapping = BuildMapping(modelBuilder);
Assert.True(databaseMapping.Model.Containers.Single().EntitySets.Any(es => es.Name == "Foos"));
}
}
namespace Fixtures
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public class StockOrder
{
public int Id { get; set; }
public int LocationId { get; set; }
public Location Location { get; set; }
public ICollection<Organization> Organizations { get; set; }
}
public class Organization
{
public int Id { get; set; }
public int StockOrderId { get; set; }
public StockOrder StockOrder { get; set; }
public ICollection<Location> Locations { get; set; }
}
public class Location
{
public int Id { get; set; }
public ICollection<StockOrder> StockOrders { get; set; }
public int OrganizationId { get; set; }
public Organization Organization { get; set; }
}
public class DecimalKey
{
public decimal Id { get; set; }
public ICollection<DecimalDependent> DecimalDependents { get; set; }
}
public class DecimalDependent
{
public int Id { get; set; }
public decimal DecimalKeyId { get; set; }
}
public abstract class SingleAbstract
{
public int Id { get; set; }
public DecimalDependent Nav { get; set; }
}
public class SplitProduct
{
public int Id { get; set; }
public string Name { get; set; }
[Required]
public SplitProductDetail Detail { get; set; }
}
public class SplitProductDetail
{
[ForeignKey("Product")]
public int Id { get; set; }
public string Name { get; set; }
[Required]
public SplitProduct Product { get; set; }
}
public class EntityWithConfiguredDuplicateColumn
{
public int Id { get; set; }
public string Description { get; set; }
[Column("Description")]
public string Details { get; set; }
}
public class EntityWithDescBase
{
public int Id { get; set; }
}
public class EntityWithDescA : EntityWithDescBase
{
public string Description { get; set; }
public ComplexWithDesc Complex { get; set; }
}
public class EntityWithDescB : EntityWithDescBase
{
public string Description { get; set; }
public ComplexWithDesc Complex { get; set; }
}
public class EntityWithDescC : EntityWithDescBase
{
public string Description { get; set; }
public ComplexWithDesc Complex { get; set; }
}
public class ComplexWithDesc
{
[Required]
public string Description { get; set; }
}
public class MaxLengthProperties
{
public string Id { get; set; }
public string Prop1 { get; set; }
public byte[] Prop2 { get; set; }
}
}
}
|