1

I’ve got a problem with Entity Framework and doing a Migration (database-update) and Foreign Key Annotations. The error message that I get is after: update-database –verbose

Applying automatic migration: 201604060846578_AutomaticMigration.
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_OutreachSet_TargetContact' AND object_id = object_id(N'[dbo].[DiagnosticsSets]', N'U'))
    DROP INDEX [IX_OutreachSet_TargetContact] ON [dbo].[DiagnosticsSets]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_OutreachSet_TargetContact' AND object_id = object_id(N'[dbo].[MotivationSets]', N'U'))
    DROP INDEX [IX_OutreachSet_TargetContact] ON [dbo].[MotivationSets]
DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.MotivationSets')
AND col_name(parent_object_id, parent_column_id) = 'TargetContact';
IF @var0 IS NOT NULL
    EXECUTE('ALTER TABLE [dbo].[MotivationSets] DROP CONSTRAINT [' + @var0 + ']')
ALTER TABLE [dbo].[MotivationSets] DROP COLUMN [TargetContact]

[...]

The object 'PK_dbo.MotivationSets' is dependent on column 'TargetContact'.
ALTER TABLE DROP COLUMN TargetContact failed because one or more objects access this column.

I’d just like to have a 0:0…1 Relationship. That's it.

I’d tried to do it just like in this example EF Code-First One-to-one relationship: Multiplicity is not valid in Role * in relationship

 public class OutreachSet
    {
        [Key]
        [Display(Name = "Target Contact")]
        public string TargetContact { get; set; }


        [Display(Name = "Next Outreach Step")]
        public string NextOutreachStep { get; set; }

        public virtual MotivationSet MotivationSet { get; set; }
}

 public class MotivationSet
    {
        [Key, ForeignKey("OutreachSet")]
        public string TargetContact { get; set; }

        public string PowerMastery { get; set; }


        [Required]
        public virtual OutreachSet OutreachSet { get; set; }
  }
Community
  • 1
  • 1
user2675973
  • 227
  • 1
  • 3
  • 8

1 Answers1

0

I think I solved it myself. I just did this: How to re-create database for Entity Framework?

If anyone still could tell me why this happend I'd be happy

Thank you!

Community
  • 1
  • 1
user2675973
  • 227
  • 1
  • 3
  • 8
  • 1
    You probably changed your model in a way migrations could not handle by default - changing the identity status, FK renames, are examples. By deleting everything and starting over you avoid the renames and everything is created from scratch. http://stackoverflow.com/questions/17894906/ef-migration-for-changing-data-type-of-columns – Steve Greene Apr 06 '16 at 15:55
  • 3
    What if you can't start from scratch? – TWilly Nov 30 '16 at 17:11