|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Data.Common; |
| 3 | +using System.Data.Entity; |
| 4 | +using System.Data.Entity.Infrastructure; |
| 5 | +using System.Data.SQLite; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | + |
| 9 | +namespace SQLite.CodeFirst.Test.IntegrationTests |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Verifies that the <see cref="CascadeOnUpdateAttribute"/> placed on a foreign key property |
| 13 | + /// results in 'ON UPDATE CASCADE' being emitted for the corresponding foreign key constraint. |
| 14 | + /// This exercises the full pipeline: the attribute is registered as a column annotation, survives |
| 15 | + /// the EF model build and is read back from the store model when the SQL is generated. |
| 16 | + /// </summary> |
| 17 | + [TestClass] |
| 18 | + public class CascadeOnUpdateTest |
| 19 | + { |
| 20 | + private static string generatedSql; |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void CascadeOnUpdateAttributeEmitsOnUpdateCascade() |
| 24 | + { |
| 25 | + using (DbConnection connection = new SQLiteConnection("FullUri=file::memory:")) |
| 26 | + { |
| 27 | + // This is important! Else the in memory database will not work. |
| 28 | + connection.Open(); |
| 29 | + |
| 30 | + using (var context = new CascadeDbContext(connection)) |
| 31 | + { |
| 32 | + // Touching the set forces the initializer (and therefore the SQL generation) to run. |
| 33 | + context.Set<Order>().FirstOrDefault(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Decorated FK with cascade-on-delete: both keywords, delete before update. |
| 38 | + StringAssert.Contains(generatedSql, "FOREIGN KEY ([CustomerId]) REFERENCES \"Customers\"([Id]) ON DELETE CASCADE ON UPDATE CASCADE"); |
| 39 | + |
| 40 | + // Decorated FK without cascade-on-delete: only the update keyword. |
| 41 | + StringAssert.Contains(generatedSql, "FOREIGN KEY ([WarehouseId]) REFERENCES \"Warehouses\"([Id]) ON UPDATE CASCADE"); |
| 42 | + |
| 43 | + // Undecorated FK: no cascade keyword at all. |
| 44 | + Assert.IsFalse(generatedSql.Contains("[RegionId]) REFERENCES \"Regions\"([Id]) ON"), |
| 45 | + "The foreign key without the attribute must not emit any cascade clause."); |
| 46 | + } |
| 47 | + |
| 48 | + private class Customer |
| 49 | + { |
| 50 | + public int Id { get; set; } |
| 51 | + } |
| 52 | + |
| 53 | + private class Warehouse |
| 54 | + { |
| 55 | + public int Id { get; set; } |
| 56 | + } |
| 57 | + |
| 58 | + private class Region |
| 59 | + { |
| 60 | + public int Id { get; set; } |
| 61 | + } |
| 62 | + |
| 63 | + private class Order |
| 64 | + { |
| 65 | + public int Id { get; set; } |
| 66 | + |
| 67 | + [CascadeOnUpdate] |
| 68 | + public int CustomerId { get; set; } |
| 69 | + public Customer Customer { get; set; } |
| 70 | + |
| 71 | + [CascadeOnUpdate] |
| 72 | + public int? WarehouseId { get; set; } |
| 73 | + public Warehouse Warehouse { get; set; } |
| 74 | + |
| 75 | + public int? RegionId { get; set; } |
| 76 | + public Region Region { get; set; } |
| 77 | + } |
| 78 | + |
| 79 | + private class CascadeDbContext : DbContext |
| 80 | + { |
| 81 | + public CascadeDbContext(DbConnection connection) |
| 82 | + : base(connection, false) |
| 83 | + { |
| 84 | + } |
| 85 | + |
| 86 | + protected override void OnModelCreating(DbModelBuilder modelBuilder) |
| 87 | + { |
| 88 | + modelBuilder.Entity<Order>() |
| 89 | + .HasRequired(o => o.Customer) |
| 90 | + .WithMany() |
| 91 | + .HasForeignKey(o => o.CustomerId) |
| 92 | + .WillCascadeOnDelete(true); |
| 93 | + |
| 94 | + modelBuilder.Entity<Order>() |
| 95 | + .HasOptional(o => o.Warehouse) |
| 96 | + .WithMany() |
| 97 | + .HasForeignKey(o => o.WarehouseId) |
| 98 | + .WillCascadeOnDelete(false); |
| 99 | + |
| 100 | + modelBuilder.Entity<Order>() |
| 101 | + .HasOptional(o => o.Region) |
| 102 | + .WithMany() |
| 103 | + .HasForeignKey(o => o.RegionId) |
| 104 | + .WillCascadeOnDelete(false); |
| 105 | + |
| 106 | + Database.SetInitializer(new CaptureInitializer(modelBuilder)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private class CaptureInitializer : SqliteInitializerBase<CascadeDbContext> |
| 111 | + { |
| 112 | + public CaptureInitializer(DbModelBuilder modelBuilder) |
| 113 | + : base(modelBuilder) |
| 114 | + { |
| 115 | + } |
| 116 | + |
| 117 | + public override void InitializeDatabase(CascadeDbContext context) |
| 118 | + { |
| 119 | + DbModel model = ModelBuilder.Build(context.Database.Connection); |
| 120 | + generatedSql = new SqliteSqlGenerator().Generate(model.StoreModel); |
| 121 | + base.InitializeDatabase(context); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments