I have a .NET4.0 application with Entity Framework 5.0 e Sql Server CE 4.0.
I have two objects with a one to one relationship (parent / child). I configured it to cascade when the parent was deleted, but for some reason it does not work.
Here is a simplified version of my entities:
public class Account { public int AccountKey { get; set; } public string Name { get; set; } public ICollection<User> Users { get; set; } } internal class AccountMap : EntityTypeConfiguration<Account> { public AccountMap() { this.HasKey(e => e.AccountKey); this.Property(e => e.AccountKey).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.Property(e => e.Name).IsRequired(); } } public class User { public int UserKey { get; set; } public string Name { get; set; } public Account Account { get; set; } public int AccountKey { get; set; } } internal class UserMap : EntityTypeConfiguration<User> { public UserMap() { this.HasKey(e => e.UserKey); this.Property(e => e.UserKey).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.Property(e => e.Name).IsRequired(); this.HasRequired(e => e.Account) .WithMany(e => e.Users) .HasForeignKey(e => e.AccountKey); } } public class TestContext : DbContext { public TestContext() { this.Configuration.LazyLoadingEnabled = false; } public DbSet<User> Users { get; set; } public DbSet<Account> Accounts { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>(); modelBuilder.LoadConfigurations(); } }
Connection string:
<connectionStrings> <add name="TestContext" connectionString="Data Source=|DataDirectory|\TestDb.sdf;" providerName="System.Data.SqlServerCe.4.0" /> </connectionStrings>
And a simplified version of the application workflow:
static void Main(string[] args) { try { Database.SetInitializer(new DropCreateDatabaseAlways<TestContext>()); using (var context = new TestContext()) context.Database.Initialize(false); Account account = null; using (var context = new TestContext()) { var account1 = new Account() { Name = "Account1^" }; var user1 = new User() { Name = "User1", Account = account1 }; context.Accounts.Add(account1); context.Users.Add(user1); context.SaveChanges(); account = account1; } using (var context = new TestContext()) { context.Entry(account).State = EntityState.Deleted; context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine("\nPress any key to exit..."); Console.ReadLine(); }
When I try to delete the parent, it throws:
The relationship cannot be changed because one or more foreign key properties are not NULL. When a relationship change occurs, the associated foreign key property is set to zero. If the foreign key does not support null values, a new relationship must be defined, another non-zero value or an object not associated with it must be assigned to the foreign key property.
I believe my relationship configuration is fine ( followed the documentation ). I also looked for recommendations for deleting individual objects .
I really cannot understand why this removal will not work. I want not to load all the children by deleting them one by one, and they delete the parent, because there must be a better solution than this.
Arthur nunes
source share